微前端(qiankun)
全局配置通用组件,自定义指令,通用方法等
const components = []
const myComponents = require.context('@/components/common', true, /.vue$/)
myComponents.keys().map(key => {
const compName = key.split('/')[1]
components.push({
name: compName,
files: require(`@/components/common/${compName}`).default
})
})
import * as parentUtils from '@/utils/util'
const directives = []
const files = require.context('@/directive/common', true, /.js$/)
files.keys().map((key) => {
const directiveName = key.split('/')[1]
directives.push({
name: directiveName,
files: require(`@/directive/common/${directiveName}`).default
})
})
const msg = {
data: store && store.getters,
components: {},
utils: parentUtils,
emitFnc: {},
state: {
message: '主应用',
time: +new Date(),
directives,
components,
parentUtils
}
}
const actions = initGlobalState(msg.state)
actions.onGlobalStateChange((state, prev) => console.log(`主应用应用监听到来自${state.from}发来消息:`, state, prev))
actions.setGlobalState(msg.state)
在子组件中使用 主应用 定义的组件,指令,函数
export async function mount(props) {
props.onGlobalStateChange((state, prev) => {
state.directives && state.directives.length && state.directives.forEach(it => {
Vue.directive(it.name, it.files)
})
state.components && state.components.forEach(it => {
Vue.component(it.name, it.files)
})
Object.keys(state.parentUtils).length && Object.keys(state.parentUtils).forEach(item => {
Vue.prototype[item] = state.parentUtils[item]
})
}, true)
instance = new Vue({
render: h => h(App)
}).$mount('#sub')
}