错误:[vuex]期望将字符串作为类型,但未定义

时间:2019-03-04 13:57:16

标签: javascript vue.js ecmascript-6 vuex

学习Vuex。我针对example projectdocument编写了一个简单的登录页面,但是当我尝试使用动作函数时,开发人员工具只是警告了我

error info

这是我的代码:

src / views / Login.vue

handleLogin (formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // to do
          this.$store.dispatch('user/login', this.loginUser)
        } else {
          ......
          })
        }
      })

src / store / index.js

import Vue from 'vue'
import Vuex from 'vuex'
import user from './modules/User/user'
// import info from './modules/info'

Vue.use(Vuex)

export default new Vuex.Store({
  strict: false,
  modules: {
    user,
    // info
  }
})

/src/store/modules/User/actions.js

export const userActions = {
  login({commit}, loginUser) {
    commit(LOGIN)
    axios.post(`${ API_BASE_USER }/login`, loginUser)
         .then(res => {
           console.log(res)
           if (res.status == 200) { commit(LOGIN_SUCCESS, res.data) }
           else { commit(LOGIN_FAILURE, res.data) }
         })
  }
}

/src/store/modules/User/user.js

import { userActions } from './actions'
import { userMutations } from './mutations'
export default {
  namespaced: true,
  state: {
    token: ''
  },
  actions: Object.assign({}, userActions),
  mutations: Object.assign({}, userMutations)
}

3 个答案:

答案 0 :(得分:2)

我明白了。 原点突变-type.js export const LOGIN = LOGIN

但是正确的mutation-type.js应该为export const LOGIN = 'LOGIN'

答案 1 :(得分:1)

在不提供参数的情况下调用$store.commit()时也会发生这种情况

答案 2 :(得分:-1)

发生类似的情况,其中Mutation名称以CAP()开头,但实际的突变以non cap()开头: RetrieveContractorSuccess:'retrieveContractorSuccess' (之前是RetrieveContractorSuccess:'RetrieveContractorSuccess')