微前端qiankun打包部署

官网:API 说明 - qiankun

前提:后台项目,在主应用设置菜单,微应用渲染组件,没有使用路由跳转loadMicroApp

1.token需要使用setGlobalState,传参或者方法用的setGlobalState

2.打包没有使用Nginx

3.有需要新打开的跳转页面写在主应用中进行跳转

主应用设置成hash

javascript 复制代码
const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})

4.方法传递:

javascript 复制代码
// 主应用
import { setGlobalState } from 'qiankun';
handler(param){}
actions.setGlobalState({
  mainMethods: {
    some: this.handler
  }
});

// 子应用,param是参数
// main.js
created() {
  onGlobalStateChange((state) => {
    this.$store.commit("setMainMethods", state.mainMethods);
  }, true);
},
// 实际页面中-(在需要打开新的菜单时使用)
const mainMethods = this.$store.state.mainMethods;
if (mainMethods && mainMethods.some) {
  mainMethods.some(param);
}

5.打包(打包花了我很长时间,不使用Nginx)

javascript 复制代码
1.主应用入口配置:
<div :id="component">
</div>
let container = component;//页面中div的组件id值,取个唯一的

let map = {}
if (process.env.NODE_ENV === 'development') {
  map = {
    file1: '8081',// 模块1
    file2: '8082', // 模块2
  };
} else if (process.env.NODE_ENV === 'production') {
  map = {
    file1: '/child/file1/index.html',// 模块1
    file2: '/child/file2/index.html', // 模块2
  };
}
// componentType是入口值,判断当前组件页面时那个微应用的,就打开哪个端口的微应用
const entry = process.env.NODE_ENV === 'development' ? `//localhost:${map[type]}` : map[type];
this.load[content] = loadMicroApp({
  name: component,//组件名称
  entry,//入口地址
  container: container,//页面中的组件渲染div
  props: {// 传参,根据需要来
    onGlobalStateChange: actions.onGlobalStateChange,
    setGlobalState: actions.setGlobalState,  },
}, {
  sandbox: { experimentalStyleIsolation: false }
});
2.微应用配置vue.config.js:
publicPath: process.env.NODE_ENV === 'development'?'/':'/child/file1/',
3.打包目录结构
└── html/                     # 根文件夹
    |
    ├── child/                # 存放所有微应用的文件夹
    |   ├── file1/            # 这个目录里面放文件就行了,不要再套一个dist
    |   ├── file2/     
    ├── index.html            # 主应用的index.html
    ├── css/                  # 主应用的css文件夹
    ├── js/                   # 主应用的js文件夹

仔细看qiankun官网就可以了,最重点的是,

我遇到如下报错:配置entry时要加上index.html(这是重点!!其他照着官网上来就可以了)

我还遇到了微应用地址报500的问题:

部署的时候会有一个配置文件(配置api代理地址),这个文件只要放在跟目录下就行了,子目录里面不要放这个文件,不然微应用会报错500

相关推荐
小成2023032026519 分钟前
Linux高级02
linux·开发语言
知行合一。。。29 分钟前
Python--04--数据容器(总结)
开发语言·python
咸鱼2.042 分钟前
【java入门到放弃】需要背诵
java·开发语言
ZK_H43 分钟前
嵌入式c语言——关键字其6
c语言·开发语言·计算机网络·面试·职场和发展
A.A呐1 小时前
【C++第二十九章】IO流
开发语言·c++
椰猫子1 小时前
Java:异常(exception)
java·开发语言
lifewange1 小时前
pytest-类中测试方法、多文件批量执行
开发语言·python·pytest
GreenTea1 小时前
一文搞懂Harness Engineering与Meta-Harness
前端·人工智能·后端
cmpxr_1 小时前
【C】原码和补码以及环形坐标取模算法
c语言·开发语言·算法
2401_827499992 小时前
python项目实战09-AI智能伴侣(ai_partner_5-6)
开发语言·python