首页 > 解决方案 > 无法读取 store.js 中未定义的属性“帖子”

问题描述

我有一个store.js - vuex被调用的动作signUserIn。SignUserIn 方法只是一个http post请求。我已导入Vue-Resourcein storefile: import VueResource from 'vue-resource'Vue.use(VueResource)并且 in main.js-store.js已导入。在我的SignIn.vue组件中,我调度SignIn操作。我得到一个错误:Cannot read property 'post' of undefined。我的导入或代码有什么问题?

行动:

signUserIn ({commit, getters}, payload) {
      let data = {
        _email: payload.email,
        _password: payload.password
      }
      let that = this
      that.$http.post(
        'url',
          data,
        { channel: 'default' },
        { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
      ).then(response => {
          const newUser = {
            email: payload.email,
            login: payload.login,
            cart: []
          }
          commit('setUser', newUser)
      }, error => {
          console.error(error);
      });
    }

突变:

setUser (state, payload) {
    state.user = payload;
}

标签: vue.jsvuexvue-resource

解决方案


$http是 Vue 实例的属性,在 vuex 商店中你应该导入 Vue 并使用它Vue.http来发出请求。

Vue.http.post(
    'url',
      data,
    { channel: 'default' },
    { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
  )
...

推荐阅读