vue2项目接入完成

This commit is contained in:
2026-06-21 00:05:19 +08:00
parent c76852f949
commit 7df8308ea8
4 changed files with 635 additions and 5 deletions
+37 -4
View File
@@ -2,7 +2,40 @@ import Vue from 'vue'
import App from './App.vue'
import router from './router'
new Vue({
router,
render: h => h(App)
}).$mount('#app')
let app = null
/**
* 挂载 Vue 实例
* 微前端环境下由主应用通过 window.mount() 调用
* 独立运行时直接调用
*/
function mount() {
if (app) return // 防止重复挂载
app = new Vue({
router,
render: h => h(App)
}).$mount('#app')
}
/**
* 卸载 Vue 实例
* 微前端 keepAlive 切换时由主应用通过 window.unmount() 调用
*/
function unmount() {
if (app) {
app.$destroy()
app = null
}
}
// 判断是否运行在 micro-app 沙箱中
if (window.__MICRO_APP_ENVIRONMENT__) {
// 注册生命周期钩子,供主应用 keepAlive 切换路由时调用
window.mount = mount
window.unmount = unmount
// 首次加载时主动挂载
mount()
} else {
// 独立运行模式(如本地开发直接访问 http://localhost:5173)
mount()
}
+2
View File
@@ -26,6 +26,8 @@ const routes = [
const router = new VueRouter({
mode: 'history', // 去掉 URL 中的 #,需要后端配合兜底
// 微前端环境下使用主应用传入的 base route,独立运行时默认 '/'
base: window.__MICRO_APP_BASE_ROUTE__ || '/',
routes
})