微前端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

相关推荐
Python大数据分析@1 分钟前
Origin、MATLAB、Python 用于科研作图,哪个最好?
开发语言·python·matlab
蓝倾1 分钟前
京东批量获取商品SKU操作指南
前端·后端·api
JSLove8 分钟前
常见 npm 报错问题
前端·npm
sunbyte8 分钟前
50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | ContentPlaceholder(背景占位)
前端·javascript·css·vue.js·tailwindcss
爱学习的茄子9 分钟前
React Hooks进阶:从0到1打造高性能Todo应用
前端·react.js·面试
知性的小mahua13 分钟前
vue3+canvas实现摄像头ROI区域标记
前端
编程零零七29 分钟前
Python巩固训练——第一天练习题
开发语言·python·python基础·python学习·python练习题
嘗_29 分钟前
暑期前端训练day5
前端
uncleTom66635 分钟前
前端布局利器:rem 适配全面解析
前端
谦哥38 分钟前
Claude4免费Vibe Coding!目前比较好的Cursor替代方案
前端·javascript·claude