qiankun 生命周期钩子
- bootstrap:只会在微应用初始化的时候调用一次,下次微应用重新进入时会直接调用 mount 钩子,不会再重复触发 bootstrap。通常我们可以在这里做一些全局变量的初始化,比如不会在 unmount 阶段被销毁的应用级别的缓存等
- mount:应用每次进入都会调用 mount 方法,通常我们在这里触发应用的渲染方法
- unmount:应用每次 切出/卸载 会调用的方法,通常在这里我们会卸载微应用的应用实例
配置主应用
https://qiankun.umijs.org/zh/guide/tutorial#%E4%B8%BB%E5%BA%94%E7%94%A8
src/router.js
import React, {useEffect} from 'react'
import {Router, Route, Switch} from 'dva/router'
import {
registerMicroApps, // 注册子应用
start, // 启动
runAfterFirstMounted, // 第一个子应用装载完毕
// setDefaultMountApp, // 设置默认装载子应用
} from 'qiankun'
import IndexPage from './routes/IndexPage'
import List from './routes/list.js'
const style = {height: 50, backgroundColor: '#008c8c'}
// 注册应用,子应用要支持跨域 fetch
const APP_CONFIG = [
{
name: 'vueApp', // 应用的名字
// 默认会加载 html,解析里面的 js动态执行
entry: '//localhost:3030',
container: '#vue', // 加载不同的应用到容器里面
activeRule: '/vue', // 激活的路径, 注意:hash的路径的匹配 /#/vue
props: {user: 'lucy'} // 在注册子应用时,传递参数给子应用,只有一次
},
{
name: 'reactApp', // 应用的名字
entry: '//localhost:3031',
container: '#react', // 加载不同的应用到容器里面
activeRule: '/react', // 激活的路径
props: {lib: 'antd'}
}
]
const APP_LIFE = {
beforeLoad: [ // 挂载前回调
app => {
console.log("before load", app)
}
],
beforeMount: [ // 挂载后回调
app => {
console.log("before mount", app)
}
],
afterUnmount: [ // 卸载后回调
app => {
console.log("after unload", app)
}
]
}
function RouterConfig({history}) {
useEffect(init, [])
function init() {
// 注册应用
registerMicroApps(APP_CONFIG, APP_LIFE)
// 设置默认子应用,参数与注册子应用时genActiveRule("/vue")内的参数一致
// setDefaultMountApp("/vue");
// 启动应用, all主应用start后,即开始预加载所有微应用静态资源
start({
prefetch: false, // 'all' 取消预加载,不点击路由,不加载
})
// 第一个子应用加载完毕回调
runAfterFirstMounted(()=>{
console.log('[MainApp] first app mounted');
});
}
return (
<>
<header style={style}>
<h1>全局的标题</h1>
</header>
<Router history={history}>
<Switch>
<Route path="/" exact component={IndexPage}/>
<Route path="/list" exact component={List}/>
</Switch>
</Router>
<div id="vue" />
<div id="react" />
</>
)
}
export default RouterConfig
src/index.js
import dva from 'dva';
import {createBrowserHistory } from 'history'
import './index.css';
// 1. Initialize
const app = dva({
history: createBrowserHistory()
});
// 2. Plugins
// app.use({});
// 3. Model
// app.model(require('./models/example').default);
// 4. Router
app.router(require('./router').default);
// 5. Start
app.start('#global-root');
public/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Dva Demo</title>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<div id="global-root"></div>
<script src="index.js"></script>
</body>
</html>
dva package.json
{
"private": true,
"scripts": {
"start": "roadhog server",
"start:no-socket": "roadhog server",
"build": "roadhog build",
"lint": "eslint --ext .js src test",
"precommit": "npm run lint"
},
"dependencies": {
"@ant-design/aliyun-theme": "0.0.4",
"@ant-design/icons": "^4.6.4",
"antd": "^4.16.3",
"bizcharts": "^4.1.5",
"dva": "^2.4.1",
"qiankun": "^2.5.0",
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
"devDependencies": {
"@babel/plugin-proposal-decorators": "^7.15.4",
"babel-plugin-dva-hmr": "^0.4.2",
"babel-plugin-import": "^2.24.2",
"eslint": "^7.32.0",
"eslint-config-umi": "^1.6.0",
"eslint-plugin-flowtype": "^6.1.0",
"eslint-plugin-import": "^2.6.0",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-react": "^7.26.0",
"husky": "^7.0.2",
"redbox-react": "^1.6.0",
"roadhog": "^2.5.0-beta.4"
}
}