【问题标题】:Cannot read property 'post' of undefined in store.js无法读取 store.js 中未定义的属性“帖子”
【发布时间】:2019-01-17 19:58:52
【问题描述】:

我在store.js - vuex 中有一个名为signUserIn 的操作。 SignUserIn 方法只是一个http post 请求。我在store 文件中导入了Vue-Resourceimport VueResource from 'vue-resource'Vue.use(VueResource) 以及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.js vuex vue-resource


    【解决方案1】:

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

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

    【讨论】:

    • @kabugh 将此答案标记为正确以关闭问题:)
    猜你喜欢
    • 1970-01-01
    • 2017-09-24
    • 1970-01-01
    • 2019-10-27
    • 2021-06-29
    • 1970-01-01
    • 2016-07-06
    • 1970-01-01
    • 2020-06-16
    相关资源
    最近更新 更多