vue中使用定时器时this指向问题

在写一个很小的demo时,用的普通函数写法,没有用es6箭头函数,发现this变化了,后来查找到了问题所在:

1 箭头函数中的this指向是固定不变(定义函数时的指向),在vue中指向vue
2 
3 普通函数中的this指向是变化的(使用函数时的指向),谁调用的指向谁

箭头函数:

1 created () {
2     setInterval(() => { console.log(this) }, 1000) // vue
3 
4     setInteval(function () { console.log(this) }, 1000) // window,因为setInterval()函数是window对象的函数
5 }

转自: https://blog.csdn.net/Mr__jin/article/details/78247695

原文地址:https://www.cnblogs.com/yangyi9343/p/9282679.html