createNamespacedHelpers
创建基于命名空间的组件绑定辅助函数。其返回一个包含 mapState、mapGetters、mapActions 和 mapMutations 的对象。它们都已经绑定在了给定的命名空间上。
长时间没用,忘了它和命名空间有联系了,特此再记录下。
dataBulletin.js
// 1 地市权限
// 2 省份权限
const cityStatus = 1
const dataBulletin = {
namespaced: true,
state: {
currentRoleStatus: 222 || cityStatus,
},
mutations: {
CHANGE_currentRoleStatus: (state, flag) => {
console.log(flag, 'flagflagflagflagflagflagflag')
state.currentRoleStatus = flag
},
},
getter: {
getCurrentRoleStatusFun(state) {
return state.currentRoleStatus
},
},
}
export default dataBulletin
index.js
import Vue from 'vue'
import Vuex from 'vuex'
import app from './modules/app'
import user from './modules/user'
import org from './modules/org'
import dataBulletin from './modules/dataBulletin'
// default router permission control
import permission from './modules/permission'
// dynamic router permission control (Experimental)
// import permission from './modules/async-router'
import getters from './getters'
Vue.use(Vuex)
export default new Vuex.Store({
modules: {
app,
user,
org,
permission,
dataBulletin,
},
state: {
},
mutations: {
},
actions: {
},
getters,
})
使用:
import {
createNamespacedHelpers } from 'vuex'
const {
mapState, mapMutations} = createNamespacedHelpers('dataBulletin')
conputed:
computed: {
...mapState({
currentRoleStatus: (state) => {
console.log(state, 'statestatestate')
return state.currentRoleStatus
},
}),
}
methods:
...mapMutations(['CHANGE_currentRoleStatus']),
官方文档链接:https://vuex.vuejs.org/zh/api/#createnamespacedhelpers