免費24小時咨詢醫(yī)生網(wǎng)站seo推廣計劃
一 主應(yīng)用改造(又稱基座改造)
1 在主應(yīng)用中安裝qiankun(npm i qiankun -S)
?2 在src下新建micro-app.js文件,用于存放所有子應(yīng)用。
const microApps = [// 當(dāng)匹配到activeRule 的時候,請求獲取entry資源,渲染到container中{name: 'micro-vue3',entry: '//localhost:40000',// 子應(yīng)用的html入口activeRule: '/vue1', // 路由匹配規(guī)則container: '#micro-vue3', // 渲染到哪里props: {},},
];export default microApps;
?3 改造vue.config.js,允許跨域訪問子應(yīng)用頁面
const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({transpileDependencies: true,devServer: {port: 8000,headers: {// 重點1: 允許跨域訪問子應(yīng)用頁面'Access-Control-Allow-Origin': '*',},},
})
?4 改造main.js
?
import { createApp } from 'vue'
import App from './App.vue'
import router from './router/router';
import ElementPlus from 'element-plus'; //element-plus
import 'element-plus/dist/index.css'; //element-plus
import { registerMicroApps, start } from 'qiankun';
import microApps from './micro-app';
// createApp(App).use(router).use(ElementPlus).mount('#app');
let app = createApp(App);
app.use(router);
app.use(ElementPlus);
app.mount('#app');// 注冊子應(yīng)用
registerMicroApps(microApps, {//還有一些生命周期 如果需要可以根據(jù)官網(wǎng)文檔按需加入beforeMount(app) {console.log('掛載前', app);},afterMount(app) {console.log('卸載后', app);},
});
console.log("hello 主應(yīng)用")
// 啟動子應(yīng)用
start({prefetch: false, //取消預(yù)加載sandbox: { experimentalStyleIsolation: true }, //沙盒模式
});
5 在App.vue中寫響應(yīng)跳轉(zhuǎn)子應(yīng)用(根據(jù)自己的項目找對應(yīng)位置寫,不局限于App.vue)
?
<template><el-menu :router="true" mode="horizontal"><!-- 子應(yīng)用的跳轉(zhuǎn)路徑 --><el-menu-item index="/index">主應(yīng)用 main</el-menu-item><el-menu-item index="/vue1">子應(yīng)用 vue3</el-menu-item></el-menu><!-- 主應(yīng)用路由渲染出口 --><router-view /><!-- 子應(yīng)用的容器 --><!-- 微前端子應(yīng)用渲染出口 --><div id="micro-vue3"></div>
</template><script>
export default {name: 'App',setup() {return {};},
};
</script><style>
html,
body,
#app {width: 100%;height: 100%;margin: 0;
}
#app {font-family: Avenir, Helvetica, Arial, sans-serif;-webkit-font-smoothing: antialiased;-moz-osx-font-smoothing: grayscale;color: #2c3e50;
}
</style>
需要注意這里的對應(yīng)關(guān)系:
?二 子應(yīng)用改造
1 在src下新建public-path.js
if (window.__POWERED_BY_QIANKUN__) {__webpack_public_path__ = window.__INJECTED_PUBLIC_PATH_BY_QIANKUN__;
}
2 改造main
?
import { createApp } from 'vue'
import App from './App.vue'
import './public-path'; // 注意需要引入public-path
// createApp(App).mount('#app')
let instance = null;
console.log("hello 子應(yīng)用")
function render({ container } = {}) {instance = createApp(App);// instance.use(router);// instance.use(store);instance.mount(container ? container.querySelector('#app2') : '#app2');
}
// 如何獨立運行微應(yīng)用?
if (!window.__POWERED_BY_QIANKUN__) {render();
}
// 子應(yīng)用接入 qiankun
// 1 導(dǎo)出三個必要的生命周期鉤子函數(shù)
// bootstrap 渲染之前
// mount 渲染函數(shù)
// unmount 卸載函數(shù)
export async function bootstrap() {// 啟動console.log("啟動")
}
export async function mount() {console.log("掛載")// 掛載render();
}
export async function unmount() {// 例如從一個子應(yīng)用到另一個子應(yīng)用 需要先卸載后再加載,防止內(nèi)存泄露console.log("卸載")// 卸載instance.unmount();instance = null; // instance.$destroy();// instance.$el.innerHTML = ''// instance = null
}
3 改造vue.config.js
const { defineConfig } = require('@vue/cli-service')
const { name } = require('./package');
console.log("hello 子應(yīng)用")
module.exports = defineConfig({transpileDependencies: true,devServer: {port: 40000,headers: {'Access-Control-Allow-Origin': '*', //開發(fā)時增加跨域 表示所有人都可以訪問我的服務(wù)器},},configureWebpack: {output: {// 必須打包出一個庫文件library: `${name}-[name]`,// 庫格式必須是 umdlibraryTarget: 'umd', // 把子應(yīng)用打包成 umd 庫格式// jsonpFunction: `webpackJsonp_${name}`,chunkLoadingGlobal: `webpackJsonp_${name}`,},},
})
?三 改造后效果
?
?