【问题标题】:Error: [vuex] expects string as the type, but found undefined错误:[vuex] 期望字符串作为类型,但发现未定义
【发布时间】:2019-07-25 20:32:10
【问题描述】:

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

这是我的代码:

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)
}

【问题讨论】:

  • 可能是namespace: true部分,应该拼写为namespaced: true
  • 它可以工作,但出现了新错误:错误:[vuex] 期望字符串作为类型,但发现未定义。

标签: javascript vue.js ecmascript-6 vuex


【解决方案1】:

我明白了。 起源mutations-type.js export const LOGIN = LOGIN

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

【讨论】:

  • 谢谢,真是个愚蠢的错误。我一直在寻找表单或有效负载中的问题,但后来我注意到我的突变类型没有定义。
【解决方案2】:

当您调用 $store.commit() 而不提供参数时也会发生这种情况

【讨论】:

  • 如果您尝试使用的常量未定义或拼写错误,会发生这种情况,需要注意...
  • @DrCord 这就是为什么你应该使用constants for mutation types
【解决方案3】:

有类似的情况,突变名称以 CAP() 开头,但实际突变以非 cap() 开头: RetrieveContractorSuccess: 'retrieveContractorSuccess' (之前是 RetrieveContractorSuccess: 'RetrieveContractorSuccess')

【讨论】:

  • 这不是答案。
  • John - 与 CAPS 不匹配时收到相同的错误消息 - 而不是引号问题 - 恕我直言,这是另一个答案
猜你喜欢
  • 2019-12-22
  • 2013-04-07
  • 2021-12-03
  • 2017-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-21
  • 1970-01-01
相关资源
最近更新 更多