应用配置基座

  1. 安装包
    npm i @umijs/pluginqiankun -D
    配置 qiankun 开启
{
  "private": true,
  "scripts": {
    "start": "umi dev",
    "build": "umi build",
    "postinstall": "umi generate tmp",
    "prettier": "prettier --write '**/*.{js,jsx,tsx,ts,less,md,json}'",
    "test": "umi-test",
    "test:coverage": "umi-test --coverage"
  },
  "gitHooks": {
    "pre-commit": "lint-staged"
  },
  "lint-staged": {
    "*.{js,jsx,less,md,json}": [
      "prettier --write"
    ],
    "*.ts?(x)": [
      "prettier --parser=typescript --write"
    ]
  },
  "dependencies": {
    "@ant-design/pro-layout": "^6.5.0",
    "react": "17.x",
    "react-dom": "17.x",
    "umi": "^3.5.41"
  },
  "devDependencies": {
    "@types/react": "^17.0.0",
    "@types/react-dom": "^17.0.0",
    "@umijs/plugin-qiankun": "^2.43.3",
    "@umijs/preset-react": "1.x",
    "@umijs/test": "^3.5.41",
    "lint-staged": "^10.0.7",
    "prettier": "^2.2.0",
    "typescript": "^4.1.2",
    "yorkie": "^2.0.0"
  }
}

  1. 注册应用
    插件构建配置应用
export default {
  qiankun: {
    master: {
      // 注册应用信息
      apps: [
        {
          name: 'app1', // 唯一 id
          entry: '//localhost:7001', // html entry
        },
        {
          name: 'app2', // 唯一 id
          entry: '//localhost:7002', // html entry
        },
      ],
    },
  },
};
  1. 装载子应用
    使用路由绑定方式
export default {
	routes: [
    {
      path: '/',
      component: '../layouts/index.js',
      routes: [
        {
          path: '/app1',
          component: './app1/index.js',
          routes: [
            {
              path: '/app1/user',
              component: './app1/user/index.js',
            },
+            // 配置微应用 app1 关联路由
+            {
+              path: '/app1/project',
+              microApp: 'app1',
+            },
          ],
        },
+       // 配置 app2 关联路由
+       {
+         path: '/app2',
+         microApp: 'app2'
+       },
        {
          path: '/',
          component: './index.js',
        },
      ],
    },
  ],
}
  1. 主应用.umirc.ts整体配置
import { defineConfig } from 'umi';

export default defineConfig({
  nodeModulesTransform: {
    type: 'none',
  },
  routes: [
    { path: '/', redirect: '/micro' },
    {
      path: '/micro',
      component: '@/pages/index',
      routes: [
        {
          path: '/micro/app1',
          microApp: 'app1',
        },
      ],
    },
    {
      path: '/home',
      component: '@/pages/Home',
    },
  ],
  fastRefresh: {},
  qiankun: {
    master: {
      // 注册子应用信息
      apps: [
        {
          name: 'app1', // 唯一 id
          entry: '//localhost:8002/micro/*', // html entry
        },
      ],
    },
  },
});

子应用配置

  1. 安装包
    npm i @umijs/plugin-qiankun -D
    配置 qiankun 开启
  2. 插件注册(config.js)
export default {
  qiankun: {
    slave: {},
  },
};
  1. 配置运行生命周期钩子(可选)
    插件自动为你创建好 qiankun 子应用需要生命周期钩子,但是如果你想在生命周期期间加一自定义逻辑可以在子应用的 src/app.ts 里导出 qiankun 对象,并实现一个生命周期钩子,其中钩子函数入参 props 由主应用自动注入
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);
  },
};

原文地址:https://blog.csdn.net/Richardjgp/article/details/134734110

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_42340.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注