-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[vue] 在vue项目中如果methods的方法用箭头函数定义结果会怎么样? #475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
this为undefined |
因为箭头函数默绑定父级作用域的上下文,所以不会绑定vue实例,所以 this 是undefind |
会使this指向的结果返回undefined |
this指向的是当前方法,并不会报错 |
this指向实例最开始实例化的那个作用域 |
var app = nvar app = new Vue({ |
虽然没有尝试过,但是我估计你是在html里面直接用<script>引入vue的,this的默认绑定在window上,而我是用的webpack构建的,默认在严格模式下,this默认绑定为undefined |
data() {
return {
log: "111"
};
},
methods: {
test: () => {
console.log(this.log); //undefined
}
} data() {
return {
log: "111"
};
},
methods: {
test() {
console.log(this.log); //111
}
} |
我的this为什么不是undefined |
在严格模式下this是undefined,在非严格模式下指向window |
问一些没用的问题 |
[vue] 在vue项目中如果methods的方法用箭头函数定义结果会怎么样?
The text was updated successfully, but these errors were encountered: