Vuex 模块 - 无法正确设置 ..mapGetters
Vuex modules - cannot set the ..mapGetters correvctly
我目前定义我的商店结构如下
store
modules
User.js
index.js
store/modules/User.js
import * as types from '@/store/mutation_types'
import firebase from 'firebase'
const state = {
...
user: JSON.parse(localStorage.getItem('user')) || null
}
const actions = {
setUser ({commit}, newUser) {
console.log('STORE ACTION setUser: ', newUser)
commit(types.SET_USER, newUser)
},
signUserIn ({commit}, payload) {
...
},
signUserUp ({commit}, payload) {
...
},
logout: context => {
...
}
}
const mutations = {
...
}
const getters = {
...
user: state => state.user
}
export const User = {
namespaced: true,
state,
actions,
mutations,
getters
}
store/index.js
/**
* Import Dependency
*/
import Vue from 'vue'
import Vuex from 'vuex'
import { firebaseMutations } from 'vuexfire'
import * as types from '@/store/mutation_types'
import { User } from '@/store/modules/User.js'
import { Account } from '@/store/modules/Account.js'
Vue.use(Vuex)
const store = new Vuex.Store({
strict: process.env.NODE_ENV !== 'production',
state: {
language: localStorage.getItem('language') || '',
...
},
actions: {
switchLanguage ({ commit }, context) {
commit(types.SET_LANGUAGE, context)
},
...
},
getters: {
language: state => state.language,
sideNav: state => state.sideNav,
showAssociation: state => state.showAssociation,
...
},
mutations: {
...
},
...firebaseMutations
},
modules: {
User,
Account
}
})
export default store
在组件中,我需要使用来自用户模块和 index.js
的 ..mapGetters
...mapGetters('User', ['user'], ['sideNav', 'showAssociation']),
但是这是不对的……我得到一个错误:
Property or method "showAssociation" is not defined
我应该如何编写我的...mapGetters?
还是我的用户模块有问题?
感谢反馈
如果你需要从不同的模块获取getters,只需分别映射它们:
computed: {
...mapGetters('User', ['user']),
...mapGetters(['showAssociation', 'sideNav'])
}
我目前定义我的商店结构如下
store
modules
User.js
index.js
store/modules/User.js
import * as types from '@/store/mutation_types'
import firebase from 'firebase'
const state = {
...
user: JSON.parse(localStorage.getItem('user')) || null
}
const actions = {
setUser ({commit}, newUser) {
console.log('STORE ACTION setUser: ', newUser)
commit(types.SET_USER, newUser)
},
signUserIn ({commit}, payload) {
...
},
signUserUp ({commit}, payload) {
...
},
logout: context => {
...
}
}
const mutations = {
...
}
const getters = {
...
user: state => state.user
}
export const User = {
namespaced: true,
state,
actions,
mutations,
getters
}
store/index.js
/**
* Import Dependency
*/
import Vue from 'vue'
import Vuex from 'vuex'
import { firebaseMutations } from 'vuexfire'
import * as types from '@/store/mutation_types'
import { User } from '@/store/modules/User.js'
import { Account } from '@/store/modules/Account.js'
Vue.use(Vuex)
const store = new Vuex.Store({
strict: process.env.NODE_ENV !== 'production',
state: {
language: localStorage.getItem('language') || '',
...
},
actions: {
switchLanguage ({ commit }, context) {
commit(types.SET_LANGUAGE, context)
},
...
},
getters: {
language: state => state.language,
sideNav: state => state.sideNav,
showAssociation: state => state.showAssociation,
...
},
mutations: {
...
},
...firebaseMutations
},
modules: {
User,
Account
}
})
export default store
在组件中,我需要使用来自用户模块和 index.js
的 ..mapGetters...mapGetters('User', ['user'], ['sideNav', 'showAssociation']),
但是这是不对的……我得到一个错误:
Property or method "showAssociation" is not defined
我应该如何编写我的...mapGetters? 还是我的用户模块有问题?
感谢反馈
如果你需要从不同的模块获取getters,只需分别映射它们:
computed: {
...mapGetters('User', ['user']),
...mapGetters(['showAssociation', 'sideNav'])
}