qiankun
主应用配置
- 安装
yarn add qiankun # 或者 npm i qiankun -S
- 在入口文件 src/main.ts 下注册微应用并启动:
import { registerMicroApps, start } from "qiankun";
registerMicroApps([
{
name: "qiankunVue",
entry: "//localhost:8080",
container: "#qiankunVue",
activeRule: "/qiankunVue",
},
]);
start();
- 在你要放置子应用的位置增加一个容器用于加载子应用
微应用配置
- 无需安装依赖
- 导出三个声明周期函数 bootstrap、mount、unmount
export async function bootstrap() {
console.log("vue app bootstraped");
}
export async function mount(props) {
console.log("vue mount", props);
render(props);
}
export async function unmount() {
console.log("vue unmount");
instance.$destroy();
instance = null;
router = null;
}
export async function update(props) {
console.log("update props", props);
}
- 在 mounted 声明周期函数中加载微应用
function render(props) {
const { container } = props;
instance = new Vue({
router,
store,
render: (h) => h(App),
}).$mount(container ? container.querySelector("#app") : "#app");
}
if (!window.__POWERED_BY_QIANKUN__) {
render();
}
- 更改微应用路由基础路径与主应用中 activeRule 同名
const router = new VueRouter({
routes,
base: "/qiankunVue",
mode: "history",
});
- 更改 webpack 的配置
const path = require("path");
module.exports = {
devServer: {
port: 8081,
historyApiFallback: true,
headers: {
"Access-Control-Allow-Origin": "*",
},
},
configureWebpack: {
output: {
library: "qiankunVue",
libraryTarget: "umd",
},
},
};
- 修改运行时的 public-path 主要解决的是微应用动态载入的 脚本、样式、图片 等地址不正确的问题。
if (window.__POWERED_BY_QIANKUN__) {
__webpack_public_path__ = window.__INJECTED_PUBLIC_PATH_BY_QIANKUN__;
}