Skip to content

1.angular基座

创建应用

bash
使用 angular-cli 先创建一个 Angular 的项目,在命令行运行如下命令:
ng new micro-base --skipTests=true  --style=less --prefix=iview --skip-install

--style=less 用于样式文件的文件扩展名或预处理器。
--skipTests=true 如果为true,则不会为新项目生成"spec.ts"测试文件。
--prefix iview 适用于初始项目的生成选择器的前缀。
--skip-install 暂时不下载依赖,我们自行npm i下载

安装

bash
cnpm i qiankun -S

路由配置要注意,主要给子应用的路由设置空组件,因为没有组件配置会报错的!

ts
ng g component components/empty //创建组件
js
const routes: Routes = [
  //这里是每个子应用的路由
  {
    path: "sub-react",
    component: EmptyComponent
  },
  {
    path: "sub-vue",
    component: EmptyComponent
  },
  {
    path: "sub-angular",
    component: EmptyComponent
  },
  {
    path: "personal-center",
    component: EmptyComponent
  },
  { path: "**", component: EmptyComponent },
];

2.react项目

创建应用

bash
create-react-app react-app

有公共的配置文件书写方法

创建qiankun.ts文件,然后暴露出一个start方法,其他的项目也是可以进行效仿的!

修改运行端口号(和angular的方式不同)

json
set PORT=3001 && script start

image-20231009153641938

其他修改方式:使用npm run eject,将配置释放出来再改

或者用rewired插件使用配置项修改端口号

3.vue项目

创建应用

bash
npm create vite@latest vue-app -- --template vue

直接按照笔记配置即可

修改端口号直接在vite.config.js里面即可

image-20231009153912032

4.angular项目

bash
ng new angular-app --skipTests=true  --style=less --prefix=iview --skip-install

https://juejin.cn/post/7154242055558922270 配置方法的文章

https://qiankun.umijs.org/zh/guide/tutorial#angular-微应用 官网的angular微应用配置文章

其实按照官网配置就可以实现,但是有一些关键点需要注意,坑比较多:

1.主要是angular.json配置文件的修改:

json
"architect": {
"build": {
  "builder": "@angular-builders/custom-webpack:browser", //这里要注意,是browser
  "options": { //在这里加options的customWebpackConfig选项
    "customWebpackConfig": {
      "path": "./custom-webpack.config.js"
    },
    "outputPath": "dist/angular-app",
    "index": "src/index.html",
    "main": "src/main.ts",
    "polyfills": "src/polyfills.ts",
    "tsConfig": "tsconfig.app.json",
    "inlineStyleLanguage": "scss",
    "assets": [
      "src/favicon.ico",
      "src/assets"
    ],
    "styles": [
      "src/styles.scss"
    ],
    "scripts": []
  },
  "configurations": {
    "production": {
      "budgets": [
        {
          "type": "initial",
          "maximumWarning": "500kb",
          "maximumError": "1mb"
        },
        {
          "type": "anyComponentStyle",
          "maximumWarning": "2kb",
          "maximumError": "4kb"
        }
      ],
      "fileReplacements": [
        {
          "replace": "src/environments/environment.ts",
          "with": "src/environments/environment.prod.ts"
        }
      ],
      "outputHashing": "all"
    },
    "development": {
      "buildOptimizer": false,
      "optimization": false,
      "vendorChunk": true,
      "extractLicenses": false,
      "sourceMap": true,
      "namedChunks": true
    }
  },
  "defaultConfiguration": "production"
},
"serve": {
  "builder": "@angular-builders/custom-webpack:dev-server", //这里要注意,是dev-server
  "configurations": {
    "production": {
      "browserTarget": "angular-app:build:production"
    },
    "development": {
      "browserTarget": "angular-app:build:development"
    }
  },
  "defaultConfiguration": "development"
},

2.安装@angular-builders/custom-webpack插件的时候,要指定版本号,一般和angular的版本一致即可!

image-20231007115630317

3.angular-router的配置文件里面,路径要和主应用的一致

image-20231007115724574

4.package.json里面的name会决定custon-webpack.config.js文件的配置内容,但是这个name是无所谓的,和主应用的一致或者和自己本项目的文件夹名字一致都可以,不会产生错误!(sub-angular和angular-app都可以的!)

image-20231007115755420

image-20231007115809255

angular项目修改端口号

在package.json文件中在命令后面加上--port 端口号即可!

image-20231007103239933

如果是hash模式有什么区别,怎么配置?

其实hash模式也是一样的,因为子应用和其他应用(其他子应用或者主应用)之间是没什么关系的

只不过如果子应用是hash模式,那么在跳转到其他应用的时候就直接跳即可,比较的方便!