微前端-qiankun:nuxt2 接入 nuxt2
创始人
2025-05-31 13:24:11
0

一、背景

主应用:nuxt2、webpack

子应用:nuxt2、webpack

二、代码-配置主应用

2.1、安装

yarn add qiankun

2.2、/plugins/qiankun.js

export default async ({ store }) => {await store.dispatch('getMenus')
}

2.3、nuxt.config.js

export default {// Disable server-side rendering: https://go.nuxtjs.dev/ssr-modessr: false,// Global page headers: https://go.nuxtjs.dev/config-headhead: {title: 'qiankun-base-main-nuxt2',htmlAttrs: {lang: 'en'},meta: [{ charset: 'utf-8' },{ name: 'viewport', content: 'width=device-width, initial-scale=1' },{ hid: 'description', name: 'description', content: '' },{ name: 'format-detection', content: 'telephone=no' }],link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }]},// Global CSS: https://go.nuxtjs.dev/config-csscss: ['element-ui/lib/theme-chalk/index.css'],// Plugins to run before rendering page: https://go.nuxtjs.dev/config-pluginsplugins: ['@/plugins/element-ui',{ src: '~/plugins/qiankun.js', ssr: false },],// Auto import components: https://go.nuxtjs.dev/config-componentscomponents: true,// Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modulesbuildModules: [// https://go.nuxtjs.dev/eslint'@nuxtjs/eslint-module'],// Modules: https://go.nuxtjs.dev/config-modulesmodules: [// https://go.nuxtjs.dev/axios'@nuxtjs/axios'],// Axios module configuration: https://go.nuxtjs.dev/config-axiosaxios: {// Workaround to avoid enforcing hard-coded localhost:3000: https://github.com/nuxt-community/axios-module/issues/308baseURL: '/'},// Build Configuration: https://go.nuxtjs.dev/config-buildbuild: {transpile: [/^element-ui/]}
}

2.4、layouts/default.vue

2.5、store/index.js

import { Message } from 'element-ui'
import {initGlobalState
} from 'qiankun'const TYPES = {INIT_APPS: 'init_apps'
}export const state = () => ({apps: [],name: 'main',sdk: null
})export const mutations = {[TYPES.INIT_APPS] (state, apps){// 初始化全局变量const actions = initGlobalState({name: state.name})// 使用 sdk 方式进行 父子应用通信, 这里大家可以根据自己项目进行增加删除const sdk = {globalState: actions,toast: (...args) => {Message(...args)},router: {push: (...args) => {this.$router.push(...args)},replace: (...args) => {this.$router.replace(...args)},resolve: (...args) => {this.$router.resolve(...args)}},store: {dispatch: (...args) => {this.dispatch(...args)},commit: (...args) => {this.commit(...args)},state: { ...this.state }},name: state.name}// 处理 apps 列表apps = apps.map((item) => {return {...item,container: '#subapp',props: {sdk}}})// 处理路由表const routes = []apps.forEach((item, i) => {if (Array.isArray(item.activeRule)) {routes.push(...item.activeRule.map(i => ({path: `${i.slice(1)}`,name: `${item.name}${i}`,component: () => import('@/pages/spa.vue').then(m => m.default || m)})))return false}routes.push({path: `${item.activeRule.slice(1)}`,name: `${item.name}-i`,component: () => import('@/pages/spa.vue').then(m => m.default || m)})})// 动态增加路由, 这里要注意 404 页面不能直接写在 pages 中// 不然匹配的时候会根据顺序匹配到 * 就直接返回了 从而匹配不到我们后续添加的动态路由// console.log(routes)this.$router.addRoutes([].concat(...routes,{path: '404',name: 404,component: () => import('~/layouts/error.vue').then(m => m.default || m)}))state.apps = appsstate.sdk = sdk}
}export const actions = {async getMenus ({ commit }) {const { payload } = await getMenus()commit(TYPES.INIT_APPS, payload)}
}function getMenus () {return {code: 200,payload: [{name: 'subapp',container: '#subapp',activeRule: '/about',entry: '//localhost:3005', // 解决不同服务器和域名props: {msg: '我是主应用main' // 主应用向微应用传递参数}}],message: 'success'}
}

三、接入子应用

微前端-qiankun:vue3-vite 接入 nuxt2_snow@li的博客-CSDN博客

四、访问qiankun主应用,访问成功

五、欢迎交流指正,关注我,一起学习。

参考链接:

qiankun从接入到部署(nuxt篇) - 掘金

相关内容

热门资讯

走进小城看消费丨江西资溪:低碳...   夏日时节下午4点,江西省抚州市资溪县大觉山景区漂流终点依然热闹。来自南昌的游客余鑫漂流结束后没有...
【中原晨会0625】市场分析专... 来源:市场资讯 (来源:中原证券研究所) 本期重点研报目录 【中原策略】市场分析:电子半导体领涨 ...
南向资金连买4日!低费率+可月... 6月25日早盘,港股红利资产震荡整理。截至11时14分,港股红利低波ETF招商(520550)下跌0...
618成交破百万!紫荆花用一套... 一年一度的618年中大促,是消费市场的晴雨表,也是品牌间最激烈的角力场。当各大品牌在直播间里铆足了劲...
原创 黄... 2026年6月25日的国际金价已经从前期的5500美元高点跌到4200美元下方,累计跌幅超过22%,...
英伟达CEO:Vera Rub... 截至9:38,中证半导体材料设备主题指数(931743)涨2.36%创新高;权重股中,中微公司涨3....
再被催债16亿!“钢铁大王”戴... 澎湃新闻记者 贺梨萍 因“铁本事件”入狱五年的戴国芳重返钢铁行业,但他并没有完成从阶下囚再到“钢铁大...
周三原油价格下跌 随着美国和伊朗在和平谈判中取得进展,越来越多的油轮公开穿越霍尔木兹海峡,原油在战时的价格上涨已经蒸发...
这种蛋白是大脑衰老的开关 这种蛋白是大脑衰老的开关 清晨,假设一位五十岁左右的王女士发现自己常常把手机放在熟悉的抽屉里又找不到...
信通院牵头算力Token出海生... 盘面上,截至11:04,中证科创创业50指数(931643)涨1.68%,创历史新高;权重股中,芯原...
海外 774 亿营收背后:日本... 文 | 游戏价值论 6月23日,彭博社报道了腾讯正在围绕出售多家日本游戏工作室少数股权开展谈判,包...
餐饮“抢人”大战:把店开到公交... 作者 |餐饮老板内参 内参君 医院、公交站、演唱会…餐饮品牌,正在无孔不入 在北京儿童医院,肯德基...
快讯 | 外资扫货!陈翊庭:港... 港交所行政总裁陈翊庭在接受《中国证券报》专访时指出,国际资本对中国资产的看法已彻底扭转,布局中国市场...
2777.77元!A股“股王”... 25日早盘,昨天创下历史新高的A股“股王”联讯仪器,今天上午继续走强,盘中股价再度刷新历史新高。 截...
原创 今... 欧洲自己的媒体直接下结论,欧盟衰退躲不掉,内部分裂拦不住,现在就连欧洲顶尖工业巨头,都偷偷在用中国的...
黄仁勋股东大会放言:本轮AI基... 在当地时间6月24日的英伟达(NVDA.O)2026年度股东大会上,股东批准了该公司全部10名董事会...
国际油价大跌 新华社消息, 纽约原油期货主力合约价格24日盘中跌破每桶70美元,为伊朗战事爆发以来首次。 市场分析...
马云带队插秧,什么信号? 一场别开生面的“务农”,让外界看到了一个不一样的阿里巴巴。 近日,阿里巴巴合伙人、高德董事长刘振飞在...
全球最大产能,最高丰度达99.... 本文转自【科技日报】; 6月23日,高丰度硼-10同位素技术暨产业化成果发布会在山东省东营市举办,全...
黄金大跳水!金饰克价年内暴跌近... 25日,现货黄金盘中震荡,截至发稿,报3985.070美元/盎司,跌0.17%。 当地时间24日,...