| 12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- import { createApp } from 'vue'
- import App from './App.vue'
- import router from './router'
- import './assets/main.css'
- import { renderWithQiankun, qiankunWindow } from 'vite-plugin-qiankun/dist/helper'
- import { restoreOriginalTitle } from '@/composables/useSyncDocumentTitle'
- let instance = null
- function render(props = {}) {
- const { container } = props
- instance = createApp(App)
- // 通过 props 接收主应用下发的内容(如设备端、token 等)
- instance.provide('microProps', props)
- instance.use(router)
- instance.mount(container ? container.querySelector('#app') : document.getElementById('app'))
- }
- // qiankun 生命周期
- renderWithQiankun({
- bootstrap() {
- console.log('[web-app] bootstrap')
- },
- mount(props) {
- console.log('[web-app] mount', props)
- render(props)
- },
- update(props) {
- console.log('[web-app] update', props)
- },
- unmount() {
- console.log('[web-app] unmount')
- // 卸载前还原宿主页面 title,避免浏览器 tab 残留子应用标题
- restoreOriginalTitle()
- instance?.unmount()
- instance = null
- },
- })
- // 独立运行时直接渲染(便于单独调试该子应用)
- if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
- render()
- }
|