export default {
name: 'HelloWorld',
data () {
return {
msg:"原始值",
}
},
methods:{
testClick:function(){
let that=this;//this指vue实例
that.msg="修改后的值";
that.$nextTick(function(){
console.log(that.$refs.aa.innerText); //保存this的指向,如果不用that保存this指向的话,嵌套函数的this指向为window,获不到vue实例的属性。
});
}
}
}
3.内置函数setTimeout()和setInterval()
第一个参数的回调函数中的this,默认指向了window
- 1.默认绑定 独立调用
- 普通函数调用,此时 this 指向 window
- 被嵌套的函数独立调用时,this默认指向了window
- 自执行函数 this默认指向了window
- 闭包中this默认指向window