TypeError: Cannot read property '$store' of undefined Vue中·無法使用this.$store訪問Vuex的全局數據


------------恢復內容開始------------

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)
   })

使用箭頭函數,
------------恢復內容結束------------


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM