You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
主应用vue: "3.4.20",vite: "4.5.2",node: 17.18.0;子应用vue: "3.2.40",vite: "4.5.13",node: 18.20.1,"vite-plugin-qiankun":"1.0.15";
通过主应用的路由访问子应用页面,
本地运行时:
主应用 http://localhost:8081/,
子应用 http://localhost:3000/,
主应用通过http://localhost:8081/bbb-web/page1 可以正常访问子应用的页面。
线上环境:
主应用和子应用都可以独立访问。
主应用地址:http://xxx.xxx.x.xxx:8000/aaa-web/login
子应用地址:http://xxx.xxx.x.xxx:8000/bbb-web/login,主子应用在同一域名下,主子应用都部署在同级目录下。
但是线上通过主应用访问子应用的页面http://xxx.xxx.x.xxx:8000/aaa-web/bbb-web/page1 页面空白无反应,浏览器只有报错
hook.js:608 single-spa minified message #1: See https://single-spa.js.org/error/?code=1
主应用appData.ts
`import { registerMicroApps } from 'qiankun'
const isDev = import.meta.env.MODE === 'development'
export const SUB_APP_PATH = '/bbb-web'
const startQiankun = (props: any) => {
const defaultProps = {
qiankunWindow: window
}
registerMicroApps([
{
name: 'bbbSub', // 子应用的名称
entry: isDev ? '//localhost:3000/' : '/bbb-web/',
activeRule: SUB_APP_PATH, // 匹配的路由
container: '#sub-app-container', // 加载的容器
props: {
...defaultProps
}
}
], {
beforeLoad: [
(app) => {
console.log('before load:', app.name)
console.log('Current location:', window.location.href)
console.log('Container exists:', !!document.querySelector('#sub-app-container'))
return Promise.resolve()
}
],
beforeMount: [
(app) => {
console.log('开始挂载:', app.name)
console.log('App entry:', app.entry)
return Promise.resolve()
}
],
afterMount: [
(app) => {
console.log('挂载成功:', app.name)
return Promise.resolve()
},
],
afterUnmount: [
(app) => {
console.log('子应用卸载:', app.name)
return Promise.resolve()
}
]
})
}
export default startQiankun`
主应用main.ts
`const app = createApp(App)
const pinia = createPinia()
app.use(pinia)
app.use(router)
startQiankun({})
app.mount('#app')
主应用路由const router = createRouter({
history: createWebHistory(
(isProd || isTest) ? '/aaa-web/' : '/'
),
routes
})`
主应用vite.config.ts
base: (isProd || isTest) ? '/aaa-web/' : '/',
子应用main.ts
import {
renderWithQiankun,
qiankunWindow,
QiankunProps
} from 'vite-plugin-qiankun/dist/helper'
let app: VueApp
const setupAll = async (props: QiankunProps = {}) => {
const { container } = props
const renderDom: string | Element =
container?.querySelector('#bbbApp') || '#bbbApp' // 避免 id 重复导致微应用挂载失败
app = createApp(App)
const pinia = createPinia()
app.use(router)
app.use(pinia)
app.use(i18n)
app.mount(renderDom)
}
renderWithQiankun({
bootstrap() {
console.log('微应用:bootstrap')
},
mount(props) {
console.log('微应用:mount', props)
setupAll(props)
},
unmount(props) {
console.log('微应用:unmount', props)
app?.unmount()
},
update(props) {
console.log('微应用:update', props)
}
})
if (!qiankunWindow.POWERED_BY_QIANKUN) {
setupAll({})
}
子应用vite.config.ts
import qiankun from 'vite-plugin-qiankun'
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), 'VITE_')
const isProduction = mode === 'production'
return {
base: isProduction ? '/bbb-web/' : '/',
plugins: [
vue(),
vueJsx(),
viteCompression({
verbose: true,
disable: false,
threshold: 10240,
algorithm: 'gzip',
ext: '.gz'
}),
createHtmlPlugin({
minify: isProduction,
inject: {
data: {
...env
}
}
}),
qiankun('bbbSub', {
// 这是主应用要指向的子应用名字
useDevMode: true
})
],
resolve: {
alias: {
'@': path.resolve(__dirname, 'src'),
'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js'
}
},
build: {
sourcemap: !isProduction
},
server: {
host: '0.0.0.0',
port: 3000
}
}
})
子应用路由
const router = createRouter({
history: createWebHistory(
qiankunWindow.POWERED_BY_QIANKUN
? '/bbb-web'
: import.meta.env.MODE === 'production'
? '/bbb-web/'
: '/'
),
routes
})
Beta Was this translation helpful? Give feedback.
All reactions