说明:
在主应用的about页面加载子应用
成果:
子应用2,点击第二个按钮
注意:如果已经渲染一个子应用,切换另一个子应用的时候先返回about页面(自动清理上一个子应用),再点击切换按钮
代码:
使用vue-cli新建vue2项目(带router)
vue create base
安装qiankun
$ yarn add qiankun # 或者 npm i qiankun -S
import Vue from 'vue'
import App from './App.vue'
import router from './router'
//引入qiankun--备份
import {registerMicroApps} from 'qiankun';
//注册app列表
const apps = [
{
name: 'vueApp',
entry: '//localhost:9999',//自动加载,解析js,动态执行,需解决跨域
container: '#cmsApp',//子应用挂载元素
activeRule: '/about/cmsApp',//激活规则,访问该规则时,挂载
props: {
a: 1
}
},
{
name: 'reactapp',
entry: '//localhost:20000',//自动加载,解析js,动态执行,需解决跨域
container: '#cmsApp',
activeRule: '/about/react'
}
]
registerMicroApps(apps);//可以按需设置生命周期方法
new Vue({
router,
render: h => h(App)
}).$mount('#app')
基座应用自己主页
基座应用自己about
基座的about
npm run serve
使用vue-cli新建vue2项目(带router)
vue create app1
import Vue from 'vue'
import App from './App.vue'
import router from './router'
let instance = null;
function render() {
instance = new Vue({
router,
render: h => h(App)
}).$mount('#app')//还是渲染到自己的html里,基座会把该html挂载
};
if (!window.__POWERED_BY_QIANKUN__) {//默认独立运行
render();
}
if (window.__POWERED_BY_QIANKUN__) {
__webpack_public_path__ = window.__INJECTED_PUBLIC_PATH_BY_QIANKUN__;
}
export async function bootstrap(props){};
export async function mount(props){
console.log(props)
render()
};
export async function unmount(props){
instance.$destroy()
};
module.exports = {
devServer: {
port:9999,
headers:{
'Access-Control-Allow-Origin':'*'
}
},
configureWebpack:{
output:{
library:'vueApp',
libraryTarget:'umd'
}
}
}
npm run serve
npx create-react-app my-app
npm install -g yarn
yarn add react-app-rewired
yarn add react-router-dom
配置应用启动、打包和跨域参数
config-overrides.js
module.exports = {
webpack:(config)=>{
config.output.library = 'reactApp';
config.output.libraryTarget = 'umd';
config.output.publicPath = 'http://localhost:20000/';
return config;
},
devServer:(configFunction)=>{
return function(proxy,allowedHost){
const config = configFunction(proxy,allowedHost);
config.headers = {
'Access-Control-Allow-Origin':'*'
}
return config;
}
}
}
...
"scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-app-rewired eject"
},
...
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
function render(){
ReactDOM.render(
,
document.getElementById('root')
);
};
if(!Window.__POWERED_BY_QIANKUN__){
render()
}
export async function bootstrap(props){};
export async function mount(props){
render()
};
export async function unmount(props){
ReactDOM.unmountComponentAtNode(document.getElementById('root'))
};
import logo from './logo.svg';
import './App.css';
function App() {
return (
Edit src/App.js
and save to reload.
Learn React
);
}
export default App;
npm start
创建react项目失败,可自行百度,index.js按照如上修改即可
Learn React
);
}
export default App;
### i. 启动
npm start
创建react项目失败,可自行百度,index.js按照如上修改即可