qiankun_Demo实践

2020-10-25  本文已影响0人  木子士心呦

主子应用对技术栈的选择

主应用:Vue + router(history)
子应用:Vue + router(history)

主应用配置

使用Vue-cli3构建主应用项目。

// 安装qiankun
cnpm install qiankun -S

// 在main.js中引入,并注册子应用配置
import {registerMicroApps,start} from 'qiankun';
registerMicroApps([
    name:'app name',
    entry:'//localhost:8001' // 子应用端口
    container:'#container', // 主应用上给子应用的容器
    activeRule:'/activeRule' // 主应用上当触发哪一个路由。才能在容器中加载该子应用
])
// 运行
start();
<template>
    <div>
        <button @click="goNext('/vue')">vue子应用</button>
        <div id="vue"></div>
    </div>
</template>
<script>
    methods:{
        goNext(router){
            window.history.pushState(null,router,router)
        }
    }
</script>

子应用配置

使用Vue-cli3构建子应用项目。
子应用不用安装qiankun,需用修改默认配置。

// vue.config.js
devServer: {
    port: '8001',
    headers: {
      'Access-Control-Allow-Origin':'*'
    }
 }
 configureWebpack: {
    output: {
      library: 'vueApp',
      libraryTarget:'umd'
    }
    
 }
// router/index.js
base:'/activeRule' 
let instance = null

function render() { 
  instance = new Vue({
    router,
    render: h => h(App)
  }).$mount('#app')
}
if (window.__POWERED_BY_QIANKUN__) { // 动态添加publicPath
  __webpack_public_path__ = window.__INJECTED_PUBLIC_PATH_BY_QIANKUN__;
}
if (!window.__POWERED_BY_QIANKUN__) { // 默认独立运行
  render();
}
export async function bootstrap() { }
export async function mount(props) { 
  render(props)
}
export async function unmount() { 
  instance.$destroy()
}
上一篇下一篇

猜你喜欢

热点阅读