npm i qiankun -S
qiankun文档官方地址:https://qiankun.umijs.org/zh/guide
import {registerMicroApps, start} from 'qiankun';
Vue.config.productionTip = false;
registerMicroApps([
{
name: 'test-web', // app name registered
entry: 'http://localhost:8081/', //项目地址
container: '#test-web',//加载组件的控件id
activeRule: '/test_web',
sanbox: true //解决css冲突
},
]);
//开启样式 主子应用样式隔离
start({sandbox: {experimentalStyleIsolation: true}});
新建一个.vue文件
<template>
//此处id与container保持一致
<div id="test-web"></div>
</template>
配置路由地址
{
path: '/test-web/*',
name: 'test-web',
component: () => import('@/views/test-web')
},
if (window.__POWERED_BY_QIANKUN__) {
// eslint-disable-next-line no-undef
__webpack_public_path__ = window.__INJECTED_PUBLIC_PATH_BY_QIANKUN__;
}
//引入public-path.js
import "./public-path";
let instance = null;
function render(props = {}) {
const {container} = props;
console.log(11111111111111, window.__POWERED_BY_QIANKUN__, '字段值')
instance = new Vue({
router,
store,
render: h => h(App),
}).$mount(container ? container.querySelector('#app') : '#app', true); //开启沙箱
}
if (!window.__POWERED_BY_QIANKUN__) {
console.log('独立运行')
render();
}
function storeTest(props) {
props.onGlobalStateChange &&
props.onGlobalStateChange(
(value, prev) => console.log(`[onGlobalStateChange - ${props.name}]:`, value, prev),
true,
);
props.setGlobalState &&
props.setGlobalState({
ignore: props.name,
user: {
name: props.name,
},
});
}
export async function bootstrap() {
console.log('111111111111 [vue] vue app bootstraped');
}
export async function mount(props) {
console.log('11111111111 [vue] props from main framework', props);
storeTest(props);
render(props);
}
export async function unmount() {
instance.$destroy();
instance.$el.innerHTML = '';
instance = null;
}
const {name} = require('./package');
module.exports = {
devServer: {
headers: {
'Access-Control-Allow-Origin': '*',
},
},
// 自定义webpack配置
configureWebpack: {
output: {
// 把子应用打包成 umd 库格式
library: `${name}-[name]`,
libraryTarget: 'umd',
//脚手架5.0 jsonpFunction 改为 chunkLoadingGlobal
jsonpFunction: `webpackJsonp_${name}`,
},
},
}
const routerConfig = {
base: window.__POWERED_BY_QIANKUN__ ? '/test-web/' : '/',
routes: []
}