vuejs mounted:无法设置 undefined 的值



我正在尝试在挂载时运行以下函数,但出现错误

"无法设置未定义的属性'天'">

以下是我的代码

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中使用datearg,但它没有在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()
}
...

最新更新