【qiankun】使用总结

主应用

主应用使用的技术栈:Angular 18 + Antd

微应用

微应用使用的技术栈:React 18 + Antd

  • 创建子应用
// 安装angular CLI(如果本地已安装可忽略)
npm install -g @angular/cli

// 创建子应用
ng new my-app-name
  • 配置子应用
  1. 安装插件 @angular-builders/custom-webpack
npm i @angular-builders/custom-webpack 
  1. 在根目录增加 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
  },
};
  1. 在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 {}

  1. 创建新模块,然后在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 {}
  1. 修改 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';
}

  1. 修改 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();
}
  1. 修改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",
       	......
        },
      }
  1. 修改 package.json文件
    在这里插入图片描述
"start": "ng serve --port 4303 --open",
  1. 启动子应用,自测是否运行成功
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值