@ttk/micro-frontend-react
v0.0.6
Published
ttk微前端react
Downloads
6
Readme
@ttk/micro-frontend-react
提供应用qiankun微前端的react组件及相关生命周期API给TTK工程使用的依赖库
1.安装
npm install -s @ttk/micro-frontend-react qiankun
或
yarn add @ttk/micro-frontend-react qiankun
2.API
2.1 mfApp
主工程加载单例子应用的组件,使用方式:
2.1.1 ttk json工程
- component/index.js引入组件,
export { mfApp as MFApp } from '@ttk/micro-frontend-react'
- app详见本工程ttk-json-demo/ttk-edf-app-micro-frontend
2.1.2 组件参数
- name: 子工程打包时output.library输出名称(一般可取package.json里的name),libraryTarget需要umd
- entry: 子应用访问url,
- startAppName:子应用指定的appName
- params: 父应用传给子应用的参数
- callbacks: 父应用传给子应用的回调
- restoreState:子应用unmount后,重新mount时,父应用传给子应用用于恢复的state
- onSaveState:父应用传给子应用的回调onSaveState,子应用unmount时,执行onSaveState,保存state到父应用
- renderLoading: 子应用加载过程中显示loading组件,不传不显示
- renderError: 子应用显示错误信息的组件,不传使用默认的
2.2 lifecycle
提供给@ttk/react框架(v4版本),作为微前端子应用工程的生命周期
import { lifecycle } from '@ttk/micro-frontend-react';
import { config as appLoaderConfig, start, render } from '@ttk/app-loader'
import { targetDomId, startAppName } from './constant'
import pack from '../package.json';
const { name: packageName } = pack;
/**
* single-SPA生命周期钩子,只会在微应用初始化的时候调用一次,下次微应用重新进入时会直接调用 mount 钩子,不会再重复触发 bootstrap。
* 通常我们可以在这里做一些全局变量的初始化,比如不会在 unmount 阶段被销毁的应用级别的缓存等。
*/
export async function bootstrap(props) {
lifecycle.bootstrap(Object.assign({ targetDomId, packageName, startAppName }, props));
let { apps, init } = await import(/* webpackPreload: true */ './apps/index');
// appLoader配置
await appLoaderConfig(init({ apps }));
}
/**
* single-SPA生命周期钩子,应用每次进入都会调用 mount 方法,通常我们在这里触发应用的渲染方法
*/
export async function mount(props = {}) {
lifecycle.mount(Object.assign({ targetDomId, packageName, startAppName , render: start }, props))
}
/**
* single-SPA生命周期钩子,应用每次 切出/卸载 会调用的方法,通常在这里我们会卸载微应用的应用实例
*/
export async function unmount(props) {
lifecycle.unmount(Object.assign({ targetDomId, packageName, startAppName , state: window.reduxStore.getState() }, props));
}
/**
* single-SPA可选生命周期钩子,仅使用 loadMicroApp 方式加载微应用时生效
*/
export async function update(props) {
lifecycle.update(Object.assign({ targetDomId, packageName, startAppName , render }, props));
}
- targetDomId为加载子应用容器元素的id,例如,应用容器元素为<div id="app"></div>,targetDomId为app
- startAppName为子应用指定的appName
- @ttk/app-loader的start, render分别为初始mount和update更新时的组件
- unmount生命周期中的参数state,传入全局state,用于获取startAppName的state,传给主工程进行保存
3.License
MIT License
Copyright (c) 2017-2021 TTK Team
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.