/store/index.js
import Vue from 'vue';
import Vuex from 'vuex';
import getters from './getters';
import user from './modules/user';
import theme from './modules/theme';
import dictionary from './modules/dictionary';
Vue.use(Vuex);
const vuex = new Vuex.Store({
state: {},
mutations: {},
actions: {},
modules: {
user,
theme,
dictionary,
},
getters
});
export default vuex
/store/getters.js
export default {
user: state => state.user,
theme: state => state.theme,
permission: state => state.user.permission
}
/store/modules/dictionary.js
import { get as get_dictionary } from "@/api/dictionary.js";
import vuex from '../index.js'
const dictionary = {
namespaced: true,
state: {
dictionaryMap: {
'yn_lawyer.group_name': []
},
},
mutations: {
SET(state, obj) {
state[obj.key] = obj.value;
},
SET_dictionaryMap(state, obj) {
console.log(obj)
state['dictionaryMap'][obj.key] = obj.value;
}
},
actions: {
get(mutations, type = "yn_lawyer.group_name") {
get_dictionary({
type,
}).then((res) => {
if (res.code === 200) {
let o = res.data.dictionary.options;
let options = o.map((v) => {
return { label: v, value: v };
});
mutations.commit('SET_dictionaryMap', { key: type, value: options })
}
}).catch(err => {
console.log('err', err)
})
},
},
// return this.$store.getters['dictionary/yn_lawyer.group_name']
getters: {
"yn_lawyer.group_name"(state) {
let v = state.dictionaryMap['yn_lawyer.group_name']
if (!v || v.length === 0) {
vuex.dispatch('dictionary/get', "yn_lawyer.group_name");
v = []
}
return v;
},
}
}
export default dictionary
网友评论