深入理解qiankun微前端路由模式与部署方案
前言
qiankun作为一款优秀的微前端框架,在实际应用中需要考虑路由模式和部署方案的选择。本文将深入探讨qiankun中不同路由模式的应用场景和注意事项,以及各种部署方案的实现细节,帮助开发者更好地构建微前端架构。
路由模式选择
基于pathname的路由区分
当主应用使用location.pathname
来区分微应用时,微应用可以采用hash
和history
两种路由模式。
注册配置示例
registerMicroApps([
{
name: 'app',
entry: 'http://localhost:8080',
container: '#container',
activeRule: '/app',
},
]);
不同框架的适配情况
-
history模式:只需设置路由的
base
即可 -
hash模式:不同框架表现不一致
| 框架 | 主应用跳转
/app/#/about
| 特殊配置 | |---------------|--------------------------|--------------------------| | Vue Router | 响应about
路由 | 无 | | React Router | 不响应about
路由 | 无 | | Angular Router| 响应about
路由 | 需要设置--base-href
|
Angular应用的特殊处理
Angular应用需要修改package.json
:
- "start": "ng serve",
+ "start": "ng serve --base-href /angular9",
- "build": "ng build",
+ "build": "ng build --base-href /angular9",
部署后可能遇到懒加载路由路径错误问题,有两种解决方案:
- 修改
public-path.js
:
__webpack_public_path__ = window.__POWERED_BY_QIANKUN__
? window.__INJECTED_PUBLIC_PATH_BY_QIANKUN__
: `http://${ip}:${port}/`;
- 修改构建命令并部署到特定目录:
- "build": "ng build",
+ "build": "ng build --base-href /angular9 --deploy-url /angular9/",
基于hash的路由区分
当所有微应用都使用hash
模式时,可以使用hash来区分微应用,主应用的路由模式不限。
注册配置示例
const getActiveRule = (hash) => (location) => location.hash.startsWith(hash);
registerMicroApps([
{
name: 'app-hash',
entry: 'http://localhost:8080',
container: '#container',
activeRule: getActiveRule('#/app-hash'),
},
]);
不同框架的适配
- React Router和Angular Router需要设置路由的
base
- Vue Router需要创建空路由页面作为父路由:
const routes = [
{
path: '/app-vue-hash',
name: 'Home',
component: Home,
children: [
// 所有其他路由写在这里
],
},
];
多微应用同时展示场景
当页面需要同时展示多个微应用且有路由跳转需求时:
- 使用
loadMicroApp
加载微应用 - 使用
memory
路由避免相互干扰:- Vue Router使用
abstract
模式 - React Router使用
memory history
模式 - Angular Router不支持此模式
- Vue Router使用
部署方案
方案一:主应用和微应用部署在同一服务器
推荐目录结构
└── html/ # 根目录
|
├── child/ # 所有微应用的目录
| ├── vue-hash/ # vue-hash微应用
| ├── vue-history/ # vue-history微应用
| ├── react-hash/ # react-hash微应用
| ├── react-history/ # react-history微应用
| ├── angular-hash/ # angular-hash微应用
| ├── angular-history/ # angular-history微应用
├── index.html # 主应用首页
├── css/ # 主应用CSS
├── js/ # 主应用JS
关键配置
| 应用 | 路由base | publicPath | 实际访问路径 | |-----------------|------------------------|--------------------------|--------------------------------------| | vue-history | /child/vue-history/ | /child/vue-history/ | http://localhost:8080/child/vue-history/ |
框架具体配置
- Vue微应用:
// vue.config.js
module.exports = {
publicPath: '/child/vue-history/',
};
// 路由配置
base: window.__POWERED_BY_QIANKUN__ ? '/app-vue/' : '/child/vue-history/',
- React微应用:
// webpack配置
module.exports = {
output: {
publicPath: '/child/react-history/',
},
};
// 路由配置
<BrowserRouter basename={window.__POWERED_BY_QIANKUN__ ? '/app-react' : '/child/react-history/'}>
- Angular微应用:
// 路由配置
providers: [
{
provide: APP_BASE_HREF,
useValue: window.__POWERED_BY_QIANKUN__ ? '/app-angular/' : '/child/angular-history/',
},
];
// package.json
"build": "ng build --deploy-url /child/angular-history/",
Nginx配置
server {
listen 8080;
server_name localhost;
location / {
root html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /child/vue-history {
root html;
index index.html index.htm;
try_files $uri $uri/ /child/vue-history/index.html;
}
}
方案二:主应用和微应用部署在不同服务器
通过Nginx代理实现跨域访问:
Nginx代理配置
/app1/ {
proxy_pass http://www.b.com/app1/;
proxy_set_header Host $host:$server_port;
}
微应用配置
// webpack配置
module.exports = {
output: {
publicPath: `/app1/`,
},
};
// 主应用注册
registerMicroApps([
{
name: 'app1',
entry: '/app1/', // http://localhost:8080/app1/
container: '#container',
activeRule: '/child-app1',
},
]);
访问控制
如需限制微应用独立访问,可通过请求头判断:
if ($http_custom_referer != "main") {
rewrite /index /404.html;
}
版本升级指南
从1.x升级到2.x版本主要变更:
-
registerMicroApps
函数:- 移除
render
参数,只需提供容器 - 新增
loader
参数显示加载状态 activeRule
参数可简写为/app
RegisterMicroAppsOpts
参数移至start
函数
- 移除
-
start
函数:jsSandbox
配置改为sandbox
- 新增
getPublicPath
和getTemplate
结语
合理选择路由模式和部署方案是构建稳定微前端架构的关键。通过本文的详细解析,开发者可以更好地理解qiankun在不同场景下的最佳实践,避免常见的坑点,实现高效、稳定的微前端应用集成。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考