

微前端(qiankun)主应用共享React组件
source link: https://www.fly63.com/article/detial/11696
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

扫一扫分享
最近需要重构一个老项目,定的方案用微前端去改造。主应用是老的项目,微应用是新的项目,由于重构时间比较紧张,子应用还需要使用父应用的一些组件。过程中遇到一些问题,记录一下。
我们知道qiankun,可以通过 props 通信传递数据,把组件通过 props 传递过去不就行了。来开始改造我们的代码
导入组件,通过props共享出去
import { registerMicroApps, start, setDefaultMountApp } from 'qiankun';
// 导入一些组件
import Custom_Date from "@date/config";
import CompanyTitle from '@title/config';
import CustomSelect from '@select/config';
import UpdateTime from '@updateTime/config';
const shareComponent = {
Custom_Date,
CompanyTitle,
CustomSelect,
UpdateTime
}
registerMicroApps([
{
name: 'child-app', // 一级市场
entry: '//localhost:7011',
container: '#childApp',
activeRule: '/page/appPM',
props: {
base: '/page/app-child/',
...shareComponent
},
},
]);
在qiankun的生命周期函数接收props,并缓存。
缓存组件工具函数
let shareMainComponent: Record<string, any> = {}
// 获取共享的组件
export const getShareMainComponent = () => {
return shareMainComponent;
}
// 设置共享的组件
export const setShareMainComponent = (currShareMainComponent: Record<string, any>) => {
for (const key in currShareMainApp) {
if (Object.prototype.hasOwnProperty.call(currShareMainComponent, key)) {
shareMainComponent[key] = currShareMainComponent[key];
}
}
}
子应用生命周期中设置共享组件
import { setShareMainComponent } from './utils/shareMainComponent';
export const qiankun = {
async bootstrap(props: any) {
console.log('app1 bootstrap', props);
},
// 应用 render 之前触发
async mount(props: any) {
setShareMainComponent(props.shareMainApp);
},
async unmount(props: any) {
console.log('app1 unmount', props);
},
};
子应用使用
import react, { FC, useEffect, useState } from 'react';
import { getShareMainComponent } from '../../../utils/shareMain';
export interface IndexConfigPageProps {
}
const IndexConfigPage: FC<IndexConfigPageProps> = props => {
const {
Custom_Date,
CompanyTitle,
CustomSelect,
UpdateTime
} = getShareMainComponent();
useEffect(() => {
}, []);
return (
<div>
<Custom_Date />
<CustomTitle />
</div>
);
}
export default IndexConfigPage;
hooks组件问题
类组件正常是没问题的,但hooks组件会有问题,报错如下

经排查分析,应该是React不是一个实例,hooks组件需要同一个实例。
借助webpack的 externals 去用同一份React。
主应用入口index.html引入react和react-dom的js文件
<script src="<%= htmlWebpackPlugin.files.publicPath %>public/react/react.development.js"></script>
<script src="<%= htmlWebpackPlugin.files.publicPath %>public/react-dom/react-dom.development.js"></script>
配置webpack的 externals ,如下
externals: {
'react': 'React',
'react-dom': 'ReactDOM'
},
主应用设置完成,下面开始配置子应用。
子应用这时就不需要引入相关的js文件,直接配置 externals ,用主应用的React和ReactDom。配置如下
externals: {
'react': 'React',
'react-dom': 'ReactDOM',
},
在此访问,问题得已解决
我们在重构巨石老项目的时候,可以考虑微前端,用微前端(qiankun)共享组件的时候,可以使用该方案。
来自:https://www.cnblogs.com/qiaojie/p/16356285.html
Recommend
-
29
写在开头 微前端系列文章: 基于 qiankun 的微前端最佳实践(万字长文) - 从 0 到 1 篇 基于 qia...
-
10
云音乐大前端专栏基于 qiankun 的 CMS 应用微前端实践2020-10-22
-
8
最近负责的新项目用到了qiankun,写篇文章分享下实战中遇到的一些问题和思考。示例代码: https://github.com/fengxianqi/qiankun-example。在线demo:
-
16
目标是最完善的微前端解决方案 - qiankun 2.0kuitos伪前端伪架构师 / mobxjs team member...
-
15
还有就是目前比较主流的几种微前端方案:基座模式:主要基于路由分发,由一个基座应用监听路由,按照路由规则去加载不同的应用,以实现应用间解耦 EMP:Webpack5 Module Federation,去中心化的微前端...
-
9
「本文已参与好文召集令活动,点击查看:后端、大前端双赛道投稿,2万元奖池等你挑战!」 📢欢迎点赞 :👍 收藏 ⭐留言 📝 如有错误敬请指正,赐人玫瑰,手留余香!...
-
23
对巨石应用说不:转转商业微前端qiankun历史项目迁移升级实践大转转FE关注微信公众号:大转转FE。 一个有趣的前端...
-
6
基于vue3.x+qiankun微前端项目搭建 - SegmentFault 思否一枚前端从业人员,偶尔写一些前端的文章,同时爱好理财,炒股。
-
4
微前端是什么 什么是微前端 Techniques, strategies and recipes for building a modern web app with multiple teams that can ship features independently
-
5
微前端(qiankun)主应用共享React组件 最近需要重构...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK