1

我正在尝试使用 Vuex 模块,但它运行不正常,我得到 uknown 动作试图调度它......

存储/index.js

import Vue from 'vue'
import Vuex from 'vuex'
import * as getters from '@/store/getters'
import { state, actions, mutations } from '@/store/root'
import authentication from '@/store/modules/authentication'

Vue.use(Vuex)

const store = new Vuex.Store({
  strict: process.env.NODE_ENV !== 'production',
  state,
  actions,
  mutations,
  getters,
  modules: {
    authentication
  }
})

export default store

存储/模块/authentication.js

import * as types from '@/store/mutation_types'
import firebase from 'firebase'

const authentication = {
  namespaced: true,
  state: {
    user: JSON.parse(localStorage.getItem('user')) || null,
    account: JSON.parse(localStorage.getItem('account')) || null
  },
  actions: {
    signUserUp ({commit}, payload) {
     ...
    },
    signUserIn ({commit}, payload) {
     ...
    },
    setUser ({commit}, newUser) {
      console.log('STORE ACTION setUser: ', newUser)
      commit(types.SET_USER, newUser)
    },
    setAccount ({commit}, newAccount) {
     ...
    },
    logout: context => {
     ...
  },
  mutations: {
   ...
  }
}

export default authentication

main.js

store.dispatch('setUser', null) // <==  [vuex] unknown action type: setUser

我的模块声明有什么问题?

4

1 回答 1

2

您正在使用namespaced模块,您应该在操作名称中包含命名空间:

store.dispatch('authentication/setUser', {});

更多的

如果您想从另一个命名空间模块调度操作,您可能需要使用{root: true}参数:

dispatch('authentication/users', {}, { root: true });

于 2018-07-15T07:22:00.170 回答