Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: [vuex] expects string as the type, but found undefined

Studying Vuex. I wrote a simple login page against the example project and the document, but when I tried to use a action function, the developer tool just warned me

error info

Here is my code:

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)
}
like image 587
Liam_1998 Avatar asked Mar 04 '19 13:03

Liam_1998


2 Answers

I got it. The origin mutations-type.js export const LOGIN = LOGIN

But the correct mutation-type.js should be export const LOGIN = 'LOGIN'

like image 54
Liam_1998 Avatar answered Oct 27 '22 13:10

Liam_1998


This can also happen when you call $store.commit() without providing it an argument

like image 30
Meme Overlord Avatar answered Oct 27 '22 11:10

Meme Overlord