首页 > 其他 > 详细

TypeError: Cannot read property '$store' of undefined Vue中·无法使用this.$store访问Vuex的全局数据

时间:2020-05-12 09:21:57      阅读:2019      评论:0      收藏:0      [点我收藏+]

------------恢复内容开始------------

methods: {
    login() {
      console.log(this.email,this.password)
      let result = toLogin(this.email,this.password)
      result.then(function(value){
        console.log(value)
        this.$store.commit(‘upStatus‘,value.status,value.message,!this.$store.flag)      //无法使用this.$store
      })
      
      // console.log(getPostById(‘5eb82f6f720a63305c760a3d‘))
    }
  },

这是因为此时的this是:undefined!在result.then这个方法改变了this的指向
解决方案:

result.then((value) => {
      console.log(value)
      this.$store.commit(‘upStatus‘,value.status,value.message,!this.$store.flag)
   })

使用箭头函数,
------------恢复内容结束------------

TypeError: Cannot read property '$store' of undefined Vue中·无法使用this.$store访问Vuex的全局数据

原文:https://www.cnblogs.com/lbzli/p/12873502.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!