[Vuejs]-How to pass a variable from a function in the component in Vue?

2👍

Because you created an new function, the this inside it will be not point to the Vue component, but to the this of the function itself.

You can use an arrow function, or save the reference of the this, then use it later.

created() {
  const self = this;

  ymaps.init(init);

  function init() {
    self.city1 = 'sdf';
  }
}

Or (better):

created() {
  const init = () => {
    this.city1 = 'sdf';
  }

  ymaps.init(init);
}

Leave a comment