/* * @Author: 廖明明 * @Date: 2022-03-31 16:36:19 * @LastEditors: 廖明明 * @LastEditTime: 2022-04-22 10:16:40 * @Description:vuex入口文件 */ import Vue from "vue"; import Vuex from "vuex"; import other from "./modules/other"; import login from "./modules/login"; import dictionary from "./modules/dictionary"; import system from "./modules/system"; Vue.use(Vuex); const myPlugin = (store) => { // 所有的模块配置(新增模块需要手动添加进去) const customModules = [{ name: 'other', aliasName: 'Other', isLocalstorage: true }, { name: 'login', aliasName: 'Login', isLocalstorage: true }, { name: 'dictionary', aliasName: 'Dictionary', isLocalstorage: false }, { name: 'system', aliasName: 'System', isLocalstorage: false }, ]; // 需要缓存的模块 const localstorageModules = customModules.filter(v => v.isLocalstorage); // 当 store 初始化后调用 let vuexStorage = uni.getStorageSync("vuexStorage"); if (vuexStorage) { customModules.forEach(v => { if (v.isLocalstorage) { store.commit(`${v.name}/resetVx${v.aliasName}`, vuexStorage[v.name]); } else { store.commit(`${v.name}/resetVx${v.aliasName}`); } }) } store.subscribe((mutation, state) => { const isLocalstorageFlag = localstorageModules.some(v => mutation.type.startsWith(`${v.name}/`)); if (isLocalstorageFlag) { const obj = {}; localstorageModules.forEach(v => { obj[v.name] = state[v.name]; }) uni.setStorageSync("vuexStorage", obj); } }); }; export default new Vuex.Store({ modules: { login, dictionary, system, other, }, plugins: [myPlugin], });