vuex存取数据
* 本文是将vuex分为好几个模块
一、首先定义假数组authorityList[]
const authorityData = {
// 命名空间开启
namespaced: true,
state: () => {
return {
authorityList: []
};
},
mutations: {
setAuthorityList: (state: { authorityList: any }, authorityList: []) => {
state.authorityList = authorityList
}
},
actions: {
setAuthorityList({ commit }: any, authorityList: any) {
commit('setAuthorityList', authorityList)
}
},
getters: {},
};
export default authorityData;
二、在store/index.ts中引用
import { createStore } from 'vuex';
import authorityData from "./authorityData";
export default createStore({
state: {
isMicroApp: (window as any).__POWERED_BY_QIANKUN__,
},
mutations: {
initIsMicroApp(state, payload){
state.isMicroApp = payload
}
},
actions: {
init({ commit }, { isMicroApp }){
commit('initIsMicroApp', isMicroApp)
}
},
getters: {},
modules: {
authorityData,
}
});
三、在App.vue里面存入数据(分级实际情况而定)
// 按钮权限获取
function getAuthorityListFun() {
// 本文中将vuex分为几个模块,方法是在某个模块中,直接通过store.commit('方法名')是获取不到的,必须要在前面加上模块名,如store.commit('模块名/方法名')才可以。
store.commit('authorityData/setAuthorityList', ['a','b',1]);
}
onMounted(()=>{
getAuthorityListFun()
})
四、在对应页面获取存入的数据
import { useStore } from 'vuex';
const store = useStore();
const authorityList = computed(() => {
return store.state.authorityData.authorityList;
});
console.log(authorityList,'authorityListauthorityList')