[英]How to use variables of data function on VueJS?
I have a stupid problem with VueJS. 我对VueJS有一个愚蠢的问题。 I'm new with VueJS.
我是VueJS的新手。 I want to access and change variables of data function.
我想访问和更改数据功能的变量。 However I couldn't do it.
但是我做不到。
Line which is getting error: 出现错误的行:
console.log('item: ' + this.item);
Error is here: 错误在这里:
TypeError: Cannot read property 'item' of undefined
TypeError:无法读取未定义的属性“ item”
Here is my code: 这是我的代码:
data: function(){
return {
item: 69,
file: 0
};
},
methods: {
toggle: (elementId = 0, type = 'item') => {
console.log('element ID: ' + elementId);
console.log('type: ' + type);
console.log('item: ' + this.item);
switch (type) {
case 'item':
break;
case 'file':
break;
}
}
}
Use toggle(elementId = 0, type = 'item') {}
instead of toggle: (elementId = 0, type = 'item') => {}
. 使用
toggle(elementId = 0, type = 'item') {}
而不是toggle: (elementId = 0, type = 'item') => {}
。
arrow function assigns this
to its parent's this
of the scope. 箭头功能分配
this
到其父的this
范围。
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#Arrow_functions_used_as_methods 请参阅: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions/Arrow_functions#Arrow_functions_used_as_methods
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.