UNPKG

qiankun-common-vue2

Version:

封装qiankun在子应用的通信方法

91 lines (83 loc) 2.35 kB
/* * @Author: Hzh * @Date: 2021-05-13 18:28:19 * @LastEditTime: 2021-05-18 17:12:25 * @LastEditors: Hzh * @Description: 封装qiankun子应用通讯 */ import { permissionDirective, checkPermission, } from "./directive/permission/index"; function setCommonData(props) { console.log("setCommonData-props", props); } /** * @description: 获取权限指令 * @param {*} store */ function getPermissionDirective(store) { return permissionDirective(store); } /** * @description: 获取权限校验方法 * @param {*} store * @param {*} value */ function getCheckPermission(store, value) { return checkPermission(store, value); } function initGlobalState(store, props = {}) { registerGlobalModule(store, props); } function registerGlobalModule(store, props = {}) { // 是否传入store及所传入的store是否为一个vuex的实例 if (!store || !store.hasModule) { return; } // 获取初始化的state const initState = (props.getGlobalState && props.getGlobalState()) || { userInfo: {}, globalConfig: {}, }; // 将父应用的数据存储到子应用中,命名空间固定为global if (!store.hasModule("global")) { const globalModule = { namespaced: true, state: initState, actions: { // 子应用改变state并通知父应用 setGlobalState({ commit }, payload) { commit("setGlobalState", payload); commit("emitGlobalState", payload); }, // 初始化,只用于mount时同步父应用的数据 initGlobalState({ commit }, payload) { commit("setGlobalState", payload); }, }, mutations: { setGlobalState(state, payload) { // eslint-disable-next-line state = Object.assign(state, payload); }, // 通知父应用 emitGlobalState(state) { if (props.setGlobalState) { props.setGlobalState(state); } }, }, }; store.registerModule("global", globalModule); } else { // 每次mount时,都同步一次父应用数据 store.dispatch("global/initGlobalState", initState); } } export default { setCommonData, initGlobalState, getPermissionDirective, getCheckPermission, };