当前位置: 首页 > news >正文

qiankun 微前端项目搭建指南(小白版)

## 你要什么

两个项目:

```

host_modele(主应用) — 外壳,路由到子应用

test_module(子应用) — 表格插件(纯展示组件)

```

## 子应用要改的文件

### 1. vue.config.js — 开跨域 + UMD

```js

module.exports = {

publicPath: '/test_entry/', // 子应用唯一标识

devServer: {

port: 8082, // 固定端口

headers: {

'Access-Control-Allow-Origin': '*', // qiankun 跨域

}

},

configureWebpack: {

output: {

libraryTarget: 'umd', // qiankun 用 UMD 加载

}

}

}

```

### 2. src/main.js — 导出 qiankun 生命周期

```js

import Vue from 'vue'

import App from './App.vue'

import createRouter from './router.js'

let instance = null

function render(props = {}) {

const { container, bus } = props

const router = createRouter()

if (bus) Vue.prototype.bus = bus // 接收主应用传来的总线

instance = new Vue({

router,

render: h => h(App)

}).$mount(container ? container.querySelector('#app') : '#app')

}

// 独立运行

if (!window.__POWERED_BY_QIANKUN__) {

render()

}

// qiankun 生命周期(三个必须)

export async function bootstrap() {}

export async function mount(props) { render(props) }

export async function unmount() {

instance.$destroy()

instance = null

}

```

### 3. src/App.vue — JS 设高度(防 qiankun bug)

```vue

<template>

<div id="app">

<router-view />

</div>

</template>

<script>

export default {

mounted() {

// 向上遍历父链,跳过零高度包装层

const el = this.$el

let p = el.parentElement

while (p && p.clientHeight === 0) p = p.parentElement

if (p && p.clientHeight > 0) {

el.style.height = p.clientHeight + 'px'

}

}

}

</script>

```

参见 [[qiankun-height-100-percent-bug]]

### 4. 路由 — 指向插件组件

```js

import Vue from 'vue'

import Router from 'vue-router'

Vue.use(Router)

export default () => new Router({

mode: 'history',

routes: [

{ path: '*', component: () => import('./views/PluginEntry.vue') }

]

})

```

### 5. PluginEntry.vue — 胶水层组件(重点)

胶水层负责三件事:收 Props、管数据、接事件。

```vue

<template>

<PureComponent v-bind="componentProps" @fetch="handleFetch" />

</template>

<script>

export default {

data() {

return {

componentData: [],

total: 0,

}

},

computed: {

pageParams() { return this.$root.pageParams || {} },

componentProps() {

return {

...(this.pageParams.Props || {}),

data: this.componentData,

total: this.total,

}

}

},

created() {

if (this.bus) {

this.bus.$on('table:data', ({ list, total }) => {

this.componentData = list

this.total = total

})

this.bus.$emit('table:fetch', { page: 1, row: 20 })

}

},

beforeDestroy() {

if (this.bus) this.bus.$off('table:data')

},

methods: {

handleFetch(params) {

if (this.bus) this.bus.$emit('table:fetch', params)

}

}

}

</script>

```

**为什么需要它:** TzxTable 是纯展示组件,不管数据从哪来。PluginEntry 接管数据和事件,让展示组件保持纯粹。[[tableentry-role-in-architecture]]

## 主应用要改的文件

### 1. App.vue — 放子应用容器

```vue

<template>

<div id="app" style="height:100vh;">

<div id="subapp-container" style="width:100%;height:100%;"></div>

</div>

</template>

```

### 2. main.js — 创建 bus、注册子应用

```js

import Vue from 'vue'

import axios from 'axios'

import { registerMicroApps, start } from 'qiankun'

// 创建事件总线(关键)

const bus = new Vue()

Vue.prototype.bus = bus

// 主应用监听数据请求,调后端

bus.$on('table:fetch', async (params) => {

try {

const res = await axios.get('/stations', { params })

if (res.data.status === 1) {

bus.$emit('table:data', {

list: res.data.data.list,

total: res.data.data.total

})

}

} catch (err) {

console.error('获取数据失败', err)

}

})

registerMicroApps([

{

name: 'my-plugin',

entry: '//localhost:8082/test_entry/',

container: '#subapp-container',

activeRule: '/test_entry/my-plugin',

props: {

bus,

params: {

Props: {

columns: [ /* 列配置 */ ],

loadMode: 'page',

pageSize: 20,

}

}

}

}

])

start()

```

## 通信流程

```

主应用

│ bus = new Vue()

│ bus.$on('table:fetch') → axios → bus.$emit('table:data')

│ 注册子应用时传 bus + Props

└→ qiankun 加载

└→ PluginEntry

│ Props + bus → 传给 PureComponent

│ 翻页 → bus.$emit('table:fetch')

│ bus.$on('table:data') → 更新数据

└→ PureComponent

只管渲染

```

## 开发生命周期

| 顺序 | 文件 | 注意 |

|------|------|------|

| 1 | vue.config.js | 跨域头 + UMD |

| 2 | main.js(子) | 三个生命周期 + bus |

| 3 | App.vue(子) | mounted 设高度 |

| 4 | PluginEntry.vue | 胶水层模板 |

| 5 | App.vue(主) | 容器 div |

| 6 | main.js(主) | bus + 注册 |

**How to apply:** 新建 qiankun 子应用项目时,按上述 6 步配置即可。核心记住:**子应用只展示,主应用只调接口,bus 在中间传话。**

http://www.jsqmd.com/news/982821/

相关文章:

  • React Hooks
  • plc 基础指令下,高级部份(官方文档整理)
  • 微信小程序计算机毕设之基于微信小程序的防诈骗服务系统设计与实现基于Springboot的防诈骗管理系统小程序(完整前后端代码+说明文档+LW,调试定制等)
  • SaltStack中state的变量
  • 榨干大模型红利:如何在实时对话场景中玩转 Prompt Caching(提示词缓存)
  • ARM Cortex-M0入门实战:LPC112x核心架构、外设驱动与低功耗设计
  • Xenia Canary:跨架构实时编译的技术革命与开源创新
  • 告别IDM试用期烦恼:开源脚本让你的下载管理体验永久免费
  • i.MX53 IPU时序配置实战:从传感器到显示的嵌入式视觉接口设计
  • MemcardRex技术解析:PS1游戏存档管理的架构设计与应用实践
  • 如何在Windows电脑上安装安卓应用:3分钟学会APK安装器的终极指南
  • KE15Z/14Z外部晶振与SWD接口硬件设计实战指南
  • 当OpenClaw遇见Linode:一键部署7×24h云端AI助理
  • K30 I2S/SAI接口时序规范与引脚复用配置实战指南
  • 3个Windows维护痛点,Dism++一站式解决指南
  • 跨境电商图片翻译工具市场报告:2026趋势与机会
  • 日记 2
  • 2026年CSDN年度技术趋势预测:AI原生、云原生与开发者工具新范式
  • GPT-4的2%激活率:MoE稀疏架构原理与工程实践
  • ​我用10年经验,总结了接地故障定位的3个核心要点​
  • 如何快速解决游戏键盘输入冲突:Hitboxer免费工具的完整指南
  • 一个报错引发的奇思妙想:用 pip install numpy==999 查看所有可用版本,这招靠谱吗?
  • 嵌入式开发时序规范解析:从SPI、I2C到I2S的硬件设计实践
  • 华硕笔记本性能调校神器:5分钟掌握G-Helper完整使用指南
  • i.MX 6SLL工业级SoC:从核心架构到硬件设计的嵌入式实战指南
  • 嵌入式学习随记
  • 别再只搜Star数了!手把手教你用GitHub Topics和高级搜索,精准发现宝藏项目
  • GetQzonehistory:如何完整备份QQ空间说说,守护你的数字记忆
  • 深入解析NXP i.MX 6系列处理器:架构、外设与嵌入式开发实战
  • i.MX RT1160电源与时钟设计:从数据手册到稳定系统的实战指南