我正在尝试在挂载时运行以下函数,但出现错误
"无法设置未定义的属性'天'">
以下是我的代码
function getDays(date) {
this.days = (new Date()).getTime() / (1000 * 24 * 60 * 60);
return parseInt(this.days) - this.START_DAYS;
}
分辨是哪条线是没有帮助的。我this.days
的原因之一是 VueJS 在使用let days
时遇到了问题
我对 VueJS 以及它如何访问变量不太熟悉。
你以这种方式声明你的方法吗?
:methods: {
getDays(date) {
this.days = new Date().getTime() / (1000 * 24 * 60 * 60);
return parseInt(this.days) - this.START_DAYS;
}
},
此外,若要在 mount 中调用该方法,请使用:this.getDays()
检查此代码沙箱以进行澄清
你应该在methods
中创建你的getDays
函数,然后你可以在mounted
中调用它
我不知道你为什么在getDays
中使用date
arg,但它没有在getDays
方法中使用
...
data() {
return {
days: ''
START_DAYS: ''
}
}
methods: {
getDays(date) {
// 'date' is never used here ?
this.days = (new Date()).getTime() / (1000 * 24 * 60 * 60);
return parseInt(this.days) - this.START_DAYS;
}
},
mounted() {
this.getDays()
}
...