赞
踩
参考官网地址
<template>
<div class="xin">
<p> 个人项目</p>
<keep-alive>
<div id="baixianHome"></div>
</keep-alive>
<!--xinHome 为放置子项目的盒子 -->
</div>
</template>
<script>
import { loadMicroApp, start } from 'qiankun';//引入手动调用方法
export default {
name: 'XinVue2',
data() {
return {
}
},
created() {
this.$nextTick(() => {
// hash模式下配置
const getActiveRule = (hash) => (location) => location.hash.startsWith(hash);
this.vueApp = loadMicroApp({
name: 'qiankun-children',
entry: '//localhost:8081/',
container: '#xinHome',
activeRule: getActiveRule('#/'),
});
//启动乾坤函数
start({ singular: false });
})
},
beforeDestroy(){
console.log( this.vueApp.unmount({ name: 'qiankun-children' }))
this.vueApp.unmount({ name: 'qiankun-children' });
},
methods: {
}
}
import Vue from 'vue'
import VueRouter from 'vue-router';
Vue.use(VueRouter)
const router = new VueRouter({
base: window.__POWERED_BY_QIANKUN__ ? '/qiankun-children/' : '/', //使用 history模式必须配置
mode: 'history',
routes: [
{
path: '/xin',
name: 'xin',
component: () => import('@/views/xin-vue2/home'),
}, {
path: '/mq',
name: 'mq',
component: () => import('@/views/mq-vue2/home'),
}
],
});
export default router
import VueRouter from 'vue-router';
import router from './router/index'
Vue.config.productionTip = false
Vue.use(VueRouter)
new Vue({
router,
render: h => h(App),
}).$mount('#appAdmin')
__webpack_public_path__ = window.__POWERED_BY_QIANKUN__
? window.__INJECTED_PUBLIC_PATH_BY_QIANKUN__
: `http://localhost:8081/`; // 填写你当前子项目的实际部署地址
import './public-path';//要放最上边
import Vue from 'vue';
import VueRouter from 'vue-router';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import App from './App.vue';
import store from './store';
import Home from './views/home/index.vue'
Vue.config.productionTip = false;
Vue.use(ElementUI);
Vue.use(VueRouter)
let router = null;
let instance = null;
function render(props = {}) {
const { container } = props;
router = new VueRouter({
scrollBehavior: () => ({ y: 0 }),
routes:[
{
path: '/home',
name: 'Home',
component: Home
}
],
});
instance = new Vue({
router,
store,
render: (h) => h(App),
}).$mount(container ? container.querySelector('#app') : '#app');
}
// 独立运行时
if (!window.__POWERED_BY_QIANKUN__) {
render();
}
export async function bootstrap() {
console.log('[vue] vue app bootstraped');
}
export async function mount(props) {
console.log('[vue] props from main framework', props);
render(props);
}
export async function unmount() {
instance.$destroy();
instance.$el.innerHTML = '';
instance = null;
router = null;
}
const { defineConfig } = require('@vue/cli-service')
const packageName = require('./package.json').name;
module.exports = defineConfig({
transpileDependencies: true,
devServer: {
headers: {
'Access-Control-Allow-Origin': '*',
},
},
configureWebpack: {
output: {
library: `${packageName}-[name]`,
libraryTarget: 'umd',
chunkLoadingGlobal: `webpackJsonp_${packageName}`,
publicPath: '/'
},
},
})
先运行主项目在运行子项目
首页效果
进入调用乾坤页面
地址:http://localhost:8081/xin#/
使用子项目路由,进入子项目home页面
地址:http://localhost:8081/xin#/home
let router = null;
let instance = null;
function render(props = {}) {
const { container } = props;
router = homeRoutes
instance = new Vue({
router,
render: (h) => h(App),
}).$mount(container ? container.querySelector('#app') : '#app');
}
// // 独立运行时
if (!window.__POWERED_BY_QIANKUN__) {
render();
}
export async function bootstrap() {
console.log('[vue] vue app bootstraped');
}
export async function mount(props) {
console.log('[vue] props from main framework', props);
render(props);
}
export async function unmount() {
instance.$destroy();
instance.$el.innerHTML = '';
instance = null;
router = null;
}
// 微应用中增加 update 钩子以便主应用手动更新微应用
export async function update(props) {
render(props);
}
static/fonts/element-icons.732389d.ttf:1 GET http://localhost:8080/static/fonts/element-icons.732389d.ttf net::ERR_ABORTED 404 (Not Found)
官网地址跳转
默认情况下沙箱可以确保单实例场景子应用之间的样式隔离,但是无法确保主应用跟子应用、或者多实例场景的子应用样式隔离。当配置为 {
strictStyleIsolation: true } 时表示开启严格的样式隔离模式。这种模式下 qiankun
会为每个微应用的容器包裹上一个 shadow dom 节点,从而确保微应用的样式不会对全局造成影响。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。