Skip to content
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

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

Open
haizhilin2013 opened this issue Jun 22, 2019 · 11 comments
Labels
vue vue

Comments

@haizhilin2013
Copy link
Collaborator

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

@haizhilin2013 haizhilin2013 added the vue vue label Jun 22, 2019
@yin-pathfinder
Copy link

this为undefined

@shanyanwt
Copy link

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

@Hellohanlili
Copy link

会使this指向的结果返回undefined

@weipxiu
Copy link

weipxiu commented Jul 4, 2019

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

@Janesummers
Copy link

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

@binnuo
Copy link

binnuo commented Jul 30, 2019

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

@yin-pathfinder
Copy link

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

虽然没有尝试过,但是我估计你是在html里面直接用<script>引入vue的,this的默认绑定在window上,而我是用的webpack构建的,默认在严格模式下,this默认绑定为undefined

@censek
Copy link

censek commented Oct 15, 2019

data() {
    return {
      log: "111"
    };
  },
methods: {
    test: () => {
      console.log(this.log); //undefined
    }
}
data() {
    return {
      log: "111"
    };
  },
methods: {
    test() {
      console.log(this.log); //111
    }
}

@nannan0109
Copy link

我的this为什么不是undefined

@Hemumu5460
Copy link

在严格模式下this是undefined,在非严格模式下指向window

@Good-XiaAo
Copy link

问一些没用的问题

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
vue vue
Projects
None yet
Development

No branches or pull requests