问题发现
我是在mounted用setInterval给方法定时,需要注意的是setInterval的指向
这样可以:
data() {
return {
timer: null
}
},
mounted() {
// *5分钟定时刷新投诉条数
this.timer = setInterval(() => {
this.getCount();
}, 300000)
},
beforeDestroy() {
clearInterval(this.timer);
},
methods: {
getCount() {
...
}
}
这样也可
setInterval(() => {
this.getCount();
}, 2000)
这样也行,注意调函数不要加括号
setInterval(this.getCount, 2000);
但是!这样不行!
setInterval(function() {
this.getCount();
}, 2000)
原因----涉及setInterval中的this指向
vue中this的指向:
箭头函数中的this指向是固定不变(定义函数时的指向),在vue中指向vue;
普通函数中的this指向是变化的(使用函数时的指向),谁调用的指向谁。
这个文章写的很好:
https://www.cnblogs.com/zsqos/p/6188835.html