TypeError: Cannot read property 'token' of undefined store dispatch vuex

TypeError: Cannot read property 'token' of undefined store dispatch vuex

我有一个使用商店 Vuex 的 vue 组件。但是我得到一个

TypeError: Cannot read property 'token' of undefined

错误。我不明白为什么。这是我的代码:

在main.js中:

import Vue from 'vue'
import Vuex from 'vuex';
import App from './App.vue'
import router from './router';
 import "./assets/css/tailwind.css";
import '@/assets/css/tailwind.css';
import store from './store';

Vue.config.productionTip = false;

 Vue.use(Vuex);

 
new Vue({
    router, store,
    render: h => h(App),
}).$mount('#app');

在store/indes.js:

import Vue from 'vue';
import Vuex from 'vuex';

 
Vue.use(Vuex);

export default new Vuex.Store({
    modules: {
    },
    state:  {
        token: ''
    }
})

在GenericForm.vue中:

  methods:  {
    execute()  {
      console.log("GenericForm.vue")

      if (this.inputFunction)  {
        this.inputFunction()
      }

      this.register()
    },

      register () {
      console.log("register()")
      try {
        const response =   AuthenticationService.register({
          email: 'testss',
          password: 'frgr'
        })
           this.$store.dispatch('setToken', response.data.token)
           this.$store.dispatch('setUser', response.data.user)
           this.$store.router.push({
          name: 'songs'
        })
      } catch (error) {
        console.log(error)
/*
        this.error = error.response.data.error
*/
      }
    }
  }

错误发生在这行代码:

 this.$store.dispatch

感谢任何帮助。

编辑:

AuthenticationService.js

import api from './api'

export default  {
    register (credentials)  {
        return api().post('register', credentials)
    }
}

api.js

import axios from 'axios'

export default()  =>  {
    return axios.create({
        baseURL: 'http://localhost:8081'
    })
};

添加console.log后:

EDIT2:

新方法:

    register: async () => {

     
      console.log("register()")
      const response = AuthenticationService.register({
        email: 'testss',
        password: 'frgr'
      }).then((response) => {

        console.log(response)
       /* this.$store.dispatch('setToken', response.data.token)
        this.$store.dispatch('setUser', response.data.user)*/
        this.$store.router.push({
          name: '/test'
        })
      });
    }

我在

上收到错误
 this.$store.router.push({
          name: '/test'
        })

行:

不过,响应可以正常记录。

意思是responsedata属性没有定义

AuthenticationService.register 方法是异步的吗?

我想是的。如果是这样,您的代码将在 response 对象被正确解析之前继续。

稍等片刻 运行 console.log(response)。如果方法是异步的,您可能会看到未解决的承诺。

否则,如果该方法没有 return 任何东西而是使用回调,您可能根本看不到任何定义。

两个个问题:

第一个问题:

此代码:

register(credentials)  {
    return api().post('register', credentials)
}

返回一个 Promise,它没有 data 属性。您想要的是访问包含在该承诺中的 axios response,因此您可以:

  • 调用 then 承诺
AuthenticationService.register({...}).then((response) => {
    console.log(response.data.token) // 'foo'
});
  • 在 Vue 组件中使用 async/await

第二题

导致存储未定义的问题是箭头函数的使用。 register() 方法不应该有箭头。一旦箭头被移除,就没有错误(定义了商店,以及路由器):

    async register() {
      console.log("register()")
      const response = AuthenticationService.register({
        email: 'testss',
        password: 'frgr'
      }).then((response) => {

        console.log(response)
        console.log(this.$store)
        this.$router.push({
          name: 'ha'
        })
      });
    }