ant.design|用umi搭建项目微服务-qiankun微服务的配置

一、父项目的搭建

// 1.新建项目 yarn create @umijs/umi-app // 2.下载依赖 yarn // 3.在packjson的devDependencies中添加qiankun依赖 yarn add @umijs/plugin-qiankun --dev // 或 npm install @umijs/plugin-qiankun --save

  • 4.main/.umirc.ts中配置
Umi 在 .umirc.ts 或 config/config.ts 中配置项目和插件
推荐在 .umirc.ts 中写配置。如果配置比较复杂需要拆分,可以放到 config/config.ts 中,并把配置的一部分拆出去,比如路由。两者二选一,.umirc.ts 优先级更高。
import { defineConfig } from 'umi'; export default defineConfig({ dva: {}, qiankun: { master: { // 注册子应用信息 apps: [ { name: 'child1', // 唯一 id entry: '//localhost:2000', // html entry // 子应用通过钩子函数的参数props可以拿到这里传入的值 props: {}, }, { name: 'child2', // 唯一 id entry: '//localhost:7100', // html entry // 子应用通过钩子函数的参数props可以拿到这里传入的值 props: {}, }, ], jsSandbox: true, // 是否启用 js 沙箱,默认为 false prefetch: true, // 是否启用 prefetch 特性,默认为 true }, }, // layout: {}, nodeModulesTransform: { type: 'none', }, routes: [ { exact: false, path: '/', component: '@/layouts/index', routes: [ { exact: true, path: '/test', component: '@/pages/test', title: 'test', }, { exact: true, path: '/child1', microApp: 'child1' }, { exact: true, path: '/child2', microApp: 'child2' }, ], }, ], });

  • 5.修改默认模板
    新建 src/pages/document.ejs,umi 约定如果这个文件存在,会作为默认模板
Your App

二、子项目的搭建
  • 1、按照父项目那样用umi新建项目
  • 2、不用引入qiankun插件依赖
  • 3、src/app.ts 目录下新建app.ts
运行时配置文件,可以在这里扩展运行时的能力,比如修改路由、修改 render 方法等。
// src/app.ts export const qiankun = { // 应用加载之前 async bootstrap(props) { console.log('app1 bootstrap', props); }, // 应用 render 之前触发 async mount(props) { console.log('app1 mount', props); }, // 应用卸载之后触发 async unmount(props) { console.log('app1 unmount', props); }, };

  • 4、.env配置端口号
PORT=2000 COMPRESS=none

启用项目,得到
ant.design|用umi搭建项目微服务-qiankun微服务的配置
文章图片

三、其他
umi 3.X
qiankun2.5.1
@umijs/preset-react": “1.x”, //作用是为了在umi项目中引入dva使用
官微demo
常见错误 【ant.design|用umi搭建项目微服务-qiankun微服务的配置】常见错误项

    推荐阅读