fe-interview icon indicating copy to clipboard operation
fe-interview copied to clipboard

[vue] 在vue项目中如果methods的方法用箭头函数定义结果会怎么样?

Open haizhilin2013 opened this issue 7 years ago • 11 comments

[vue] 在vue项目中如果methods的方法用箭头函数定义结果会怎么样?

haizhilin2013 avatar Jun 22 '19 21:06 haizhilin2013

this为undefined

yin-pathfinder avatar Jun 28 '19 08:06 yin-pathfinder

因为箭头函数默绑定父级作用域的上下文,所以不会绑定vue实例,所以 this 是undefind

shanyanwt avatar Jul 02 '19 03:07 shanyanwt

会使this指向的结果返回undefined

Hellohanlili avatar Jul 03 '19 10:07 Hellohanlili

this指向的是当前方法,并不会报错

weipxiu avatar Jul 04 '19 08:07 weipxiu

this指向实例最开始实例化的那个作用域

Janesummers avatar Jul 23 '19 10:07 Janesummers

var app = nvar app = new Vue({ el: "#app", data: {}, methods: { a: () => { console.log(this); //Window } } }); 为什么我的this指向的是Window?

binnuo avatar Jul 30 '19 08:07 binnuo

var app = nvar app = new Vue({ el: "#app", data: {}, methods: { a: () => { console.log(this); //Window } } }); 为什么我的this指向的是Window?

虽然没有尝试过,但是我估计你是在html里面直接用

Owner


Other Repo Issues