代码之家  ›  专栏  ›  技术社区  ›  kabugh

无法读取store.js中未定义的属性“post”

  •  0
  • kabugh  · 技术社区  · 7 年前

    我有行动 store.js - vuex 打电话 signUserIn . SignUserIn方法只是 http post 请求。我已经进口了 Vue-Resource 在里面 store 文件: import 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;
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Max Sinev    7 年前

    $http 是Vue实例的属性,应在Vue存储中导入Vue并使用 Vue.http 提出请求。

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