Ask Your Question
3

In Vue.js, how can a variable container be updated within a JavaScript function that is not a method of Vue.js?

asked 2021-04-13 11:00:00 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-01-25 14:00:00 +0000

plato gravatar image

One option is to access the Vue instance through the global reference app and update the variable container using Vue's reactivity system.

For example:

// define Vue instance
var app = new Vue({
  data: {
    myVariable: 'initial value'
  }
});

// define a JavaScript function that is not a method of Vue.js
function myFunction() {
  // access the Vue instance
  app.myVariable = 'new value';
}

// call the function to update the variable container
myFunction();

In this example, app is the global reference to the Vue instance. The myFunction function accesses app and updates the myVariable data property using Vue's reactivity system. The updated value can then be accessed from any methods or templates within the Vue instance.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-04-13 11:00:00 +0000

Seen: 11 times

Last updated: Jan 25 '22