【qiankun】使用总结
主应用
主应用使用的技术栈:Angular 18 + Antd
微应用
微应用使用的技术栈:React 18 + Antd
- 创建子应用
// 安装angular CLI(如果本地已安装可忽略)
npm install -g @angular/cli
// 创建子应用
ng new my-app-name
- 配置子应用
- 安装插件 @angular-builders/custom-webpack
npm i @angular-builders/custom-webpack
- 在根目录增加 custom-webpack.config.js文件
const appName = require('./package.json').name;
module.exports = {
devServer: {
headers: {
'Access-Control-Allow-Origin': '*',
},
},
output: {
library: `${appName}-[name]`,
libraryTarget: 'umd',
chunkLoadingGlobal: `webpackJsonp_${appName}`, // webpack 5 需要把 jsonpFunction 替换成 chunkLoadingGlobal
},
};
- 在app文件夹下新增 app.module.ts文件和删除 app.config.ts文件
app.module.ts
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app.routes';
import { CustomerRefundComponent } from './customer-refund/customer-refund.component';
@NgModule({
declarations: [AppComponent,],
imports: [BrowserModule,AppRoutingModule, CustomerRefundComponent],
bootstrap: [AppComponent],
providers: []
})
export class AppModule {}
- 创建新模块,然后在app.routes.ts中配置路由
app.routes.ts
import { RouterModule, Routes } from '@angular/router';
import { CustomerRefundComponent } from './customer-refund/customer-refund.component';
import { NgModule } from '@angular/core';
import { APP_BASE_HREF } from '@angular/common';
export const routes: Routes = [
{
path: 'customer-refund/add', component: CustomerRefundComponent // 新模块路由
},
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
providers: [{ provide: APP_BASE_HREF, useValue: (window as any).__POWERED_BY_QIANKUN__ ? '/apps/raise/mycustomer' : '/' }]
})
export class AppRoutingModule {}
- 修改 index.html 、app.component.html和app.component.ts的内容
- index.html 给app-root添加唯一标识 mycustomerroot
<app-root id="mycustomerroot"></app-root>
- app.component.html 删除不需要的内容,保留路由入口
<router-outlet />
- app.component.ts 注释或者删除 standalone 和 imports属性,selector属性值添加标签id
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
@Component({
selector: 'app-root #mycustomerroot', // 添加标签id
// standalone: true, // 注释
// imports: [RouterOutlet], //注释
templateUrl: './app.component.html',
styleUrl: './app.component.scss'
})
export class AppComponent {
title = 'MyCustomer';
}
- 修改 main.js内容
import { AppModule } from './app/app.module';
import { NgModuleRef } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
// 删除或者注释
// bootstrapApplication(AppComponent, appConfig)
// .catch((err) => console.error(err));
// 以下内容为新增内容
let app: void | NgModuleRef<AppModule>;
async function render(props?: any) {
app = await platformBrowserDynamic()
.bootstrapModule(AppModule, { ngZone: (window as any).ngZone })
.catch((err) => console.error(err));
console.log(`子应用数据接收`, props);
}
if (!(window as any).__POWERED_BY_QIANKUN__) {
render();
}
export async function bootstrap(props: Object) {
console.log(props);
}
export async function mount(props: Object) {
render(props);
}
export async function unmount(props: Object) {
console.log(props);
// @ts-ignore
app.destroy();
}
- 修改angular.json文件
architect > build > builder 和 architect > serve > builder 的值改为我们安装的插件,将我们的打包配置文件加入到 [packageName] > architect > build > options。
"architect": {
"build": {
"builder": "@angular-builders/custom-webpack:browser",
"options": {
"customWebpackConfig": {
"path": "./custom-webpack.config.js"
},
.......
},
"main": "src/main.ts", // 参数名改成main 不然会报错找不到入口文件
},
.....
"serve": {
"builder": "@angular-builders/custom-webpack:dev-server",
......
},
}
- 修改 package.json文件
"start": "ng serve --port 4303 --open",
- 启动子应用,自测是否运行成功