箭头函数this指向

在回调函数中:具有代表性的为axios的回调

普通函数
let _this=this
axios.get('/user', {
    params: {
    }
  })
  .then(function (response) {
    console.log(_this.$route.name) //使用_this指向vue实例,可以获取到name
  })
  .catch(function (error) {
    console.log(_this.$route.name) //使用_this指向vue实例,可以获取到name
  });
箭头函数
axios.get('/user', {
    params: {
    }
  })
  .then(response => {    //箭头函数中this指向vue实例
    console.log(this.$route.name)
  })
  .catch(error => {      //箭头函数中this指向vue实例
    console.log(this.$route.name)
  });

文章参考:https://www.jianshu.com/p/f722e3667cf6

posted @ 2020-12-01 10:34  阿狸小小云的快乐时光  阅读(80)  评论(0编辑  收藏  举报