[Vuejs]-Get vanillajs value to display in vuejs model so it is accessible in method

0👍

Scopes in JS

What you have here is a beginner problem with scopes

Doesn’t work

function example() {
  // This variable is defined inside of this function
  // So if you try to access it out of this function,
  // it will throw an error and you cannot access it
  const data = [];
}

example();
console.log(data); // ReferenceError: data is not defined

Works

function example() {
  window.data = { hello: "world" }
}
example();

console.log(data);        // { hello: "world" }
console.log(window.data); // { hello: "world" }

What you should actually do

Functions can return values, so that problems like these don’t occur. Make use of it like I’ve shown below. I’ve shown a generic example, but you can use the concept in your mounted handler and getData function

function example() {
  const data = [{ a: "b" }]; // Compute your data
  return data;
}

const list = example();
console.log(list)

Leave a comment