如何从 Vue 组件中的函数传递变量?
How to pass a variable from a function in the component in Vue?
如何在Vue中从组件中的函数传递变量?
这是我的代码:
export default {
name: 'app',
data: function () {
return{
city1: '',
city2: '',
metr: 0
}
},
created () {
ymaps.ready(init);
function init() {
$data.city1 = 'sdf'; // ?this to data of component?
因为你新建了一个函数,里面的this
不会指向Vue组件,而是指向函数本身的this
你可以使用一个arrow function,或者保存this
的引用,以后再用。
created() {
const self = this;
ymaps.init(init);
function init() {
self.city1 = 'sdf';
}
}
或(更好):
created() {
const init = () => {
this.city1 = 'sdf';
}
ymaps.init(init);
}
如何在Vue中从组件中的函数传递变量? 这是我的代码:
export default {
name: 'app',
data: function () {
return{
city1: '',
city2: '',
metr: 0
}
},
created () {
ymaps.ready(init);
function init() {
$data.city1 = 'sdf'; // ?this to data of component?
因为你新建了一个函数,里面的this
不会指向Vue组件,而是指向函数本身的this
你可以使用一个arrow function,或者保存this
的引用,以后再用。
created() {
const self = this;
ymaps.init(init);
function init() {
self.city1 = 'sdf';
}
}
或(更好):
created() {
const init = () => {
this.city1 = 'sdf';
}
ymaps.init(init);
}