微前端-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篇) - 掘金

相关内容

热门资讯

香港金融管理局对三家银行采取纪... 北京商报讯(记者 孟凡霞 实习记者 周义力)7月22日,香港金融管理局宣布,根据《打击洗钱及恐怖分子...
原创 突... 美国突然变卦了!就在中美贸易谈判进入关键阶段,美国财长贝森特冷不防抛出新要求——打算在协议里硬塞两条...
深圳新晋13家独角兽企业,福田... 7月18日,2025中国(深圳)独角兽企业大会发布《GEI中国独角兽企业研究报告2025》和《深圳市...
汇划1180亿元!“跨境理财通... 7月21日,记者从人民银行广东省分行获悉,“跨境理财通”自2021年启动以来,极大地促进了粤港澳大湾...
秦洪看盘|热门主线波动不改A股... 来源:澎湃新闻 周二A股市场出现了探底回稳的走势。 早盘由于银行股的疲软,主要股指一度翻绿,升势有受...
股票行情快报:美凯龙(6018... 证券之星消息,截至2025年7月22日收盘,美凯龙(601828)报收于3.07元,上涨0.66%,...
原创 煜... 在全球能源格局加速演变、国内“双碳”目标持续深化的大背景下,电力行业正站在变革的前沿。2025年是“...
星环科技拟港交所上市:年亏损额... 近日,科创板上市公司星环科技(688031.SH)发布公告称,为进一步提高公司综合竞争力,更好地利用...
中国启动水电珠峰水利隧道工程!... 2025年7月,中国雅江集团的成立和雅鲁藏布江下游水电工程的开工,标志着世界水电史迎来新的里程碑。 ...
OpenAI CEO:Deep... 【环球网科技综合报道】7月22日消息,据windowscentral报道称,OpenAI 首席执行官...
国家外汇局贾宁:未来外资配置人... 7月22日,国家外汇管理局国际收支司司长贾宁在国新办新闻发布会上表示,未来外资配置人民币资产仍具有较...
并购 | 深度拆解地方国资收购... 一、交易背景 2023年9月20日晚间,上海雅仕投资发展股份有限公司(以下简称“上海雅仕”)公告称...
21日豆二上涨0.19%,最新... 来源:新浪期货 新浪期货 根据交易所数据,截至7月21日收盘主力合约豆二2509,涨跌+0.19%,...
兴证资管国企红利优选混合发起式... AI基金兴证资管国企红利优选混合发起式A(023169)披露2025年二季报,第二季度基金利润112...
专访新辰商业集团联合创始人及C... 在当前商业环境持续变革的背景下,社区商业正逐步成为商业领域的重要发展方向。新辰商业作为这一领域的后起...
与美国斗了整整七年,中国总结出... 与美国斗了整整七年,中方总结出4句话,想看美国是否吸取了教训 国新办日前召开系列主题新闻发布会,商务...
以“诚”破局,利他共生——稻盛... 在中国民营经济迈向高质量发展的关键阶段,企业呼唤更加坚定的精神信仰与先进的经营哲学。6月8日,稻盛和...
詹记桃酥频现日期乱象,有消费者... 近日,有消费者爆料称,其在连锁中式糕点品牌詹记桃酥(下称“詹记”)门店买到了“未来蛋糕”,门店回应称...
李开复:零一万物AI Agen... 李开复入场做AI Agent智能体。 7月22日消息,零一万物创始人兼CEO李开复博士今天上午宣布,...