微前端qiankun主應(yīng)用與子應(yīng)用之間的跳轉(zhuǎn)示例
具體需求
一個(gè)后臺(tái)管理系統(tǒng),子應(yīng)用中的token時(shí)效后,接口請(qǐng)求報(bào)錯(cuò),這時(shí)候需要跳轉(zhuǎn)到主應(yīng)用中的登錄頁面。
傳遞一個(gè)登錄方法
在主應(yīng)用調(diào)用子應(yīng)用
傳遞一個(gè)登錄方法,在有需要的地方調(diào)用該方法。
import { registerMicroApps, start } from 'qiankun';
import router from '@/router'
const apps = [
{
name: 'sonApp',
entry: '//localhost:8080',
container: '#container',
activeRule: '/son-app',
}
]
registerMicroApps(apps,{
// qiankun 生命周期鉤子 - 加載前
beforeLoad: (app) => {
console.log('加載子應(yīng)用前,加載進(jìn)度條=', app.name)
const data = {
token: 'admin',
}
app.props.data = data
// 退出方法
app.props.reRegister = () => {
store.dispatch('LogOut').then(() => {
sessionStorage.removeItem('tabViews')
location.reload()
console.log('重新登錄~')
})
}
return Promise.resolve()
},
// qiankun 生命周期鉤子 - 掛載后
afterMount: (app) => {
console.log('加載子應(yīng)用前,進(jìn)度條加載完成', app.name)
return Promise.resolve()
}
} );
// 啟動(dòng) qiankun
start();子應(yīng)用接收主應(yīng)用傳遞的參數(shù)和方法
并在有需要的地方使用Vue.prototype.$baseReRegister()
import './public-path';
import Vue from 'vue';
import VueRouter from 'vue-router';
import App from './App.vue';
import routes from './router';
import store from './store';
Vue.config.productionTip = false;
let router = null;
let instance = null;
function render(props = {}) {
const { container, mainRouter } = props;
router = new VueRouter({
base: window.__POWERED_BY_QIANKUN__ ? '/app-vue/' : '/',
mode: 'history',
routes,
});
instance = new Vue({
router,
store,
render: (h) => h(App),
}).$mount(container ? container.querySelector('#app') : '#app');
// 將主應(yīng)用的函數(shù)掛到原生上方便調(diào)用
Vue.prototype.$baseReRegister = reRegister
}
// 獨(dú)立運(yùn)行時(shí)
if (!window.__POWERED_BY_QIANKUN__) {
render();
}
export async function bootstrap() {
console.log('[vue] vue app bootstraped');
}
export async function mount(props) {
console.log('[vue] props from main framework', props);
render(props);
}
export async function unmount() {
instance.$destroy();
instance.$el.innerHTML = '';
instance = null;
router = null;
}通過history.pushState()方式進(jìn)行跳轉(zhuǎn)
window.history.pushState({
user: {}
}, '', '/login')}
主應(yīng)用開啟qiankun并向子應(yīng)用傳遞數(shù)據(jù)
將主應(yīng)用的路由在吊起子應(yīng)用時(shí)傳遞過去,使用主應(yīng)用的路由完成跳轉(zhuǎn)。但是嘗試未能成功,有采用這條思路做對(duì)的小伙伴可以給個(gè)建議。
主應(yīng)用開啟qiankun并向子應(yīng)用傳遞數(shù)據(jù)
import { registerMicroApps, start } from 'qiankun';
import router from '@/router'
const apps = [
{
name: 'sonApp',
entry: '//localhost:8080',
container: '#container',
activeRule: '/son-app',
}
]
registerMicroApps(apps,{
// qiankun 生命周期鉤子 - 加載前
beforeLoad: (app) => {
console.log('加載子應(yīng)用前,加載進(jìn)度條=', app.name)
const data = {
token: 'admin',
}
app.props.data = data
// 向子應(yīng)用傳遞路由
app.props.mainRouter = router
return Promise.resolve()
},
// qiankun 生命周期鉤子 - 掛載后
afterMount: (app) => {
console.log('加載子應(yīng)用前,進(jìn)度條加載完成', app.name)
return Promise.resolve()
}
} );
// 啟動(dòng) qiankun
start();子應(yīng)用接收數(shù)據(jù),在需要更改到主路由的地方使用Vue.prototype.parentRouter
import './public-path';
import Vue from 'vue';
import VueRouter from 'vue-router';
import App from './App.vue';
import routes from './router';
import store from './store';
Vue.config.productionTip = false;
let router = null;
let instance = null;
function render(props = {}) {
const { container, mainRouter } = props;
router = new VueRouter({
base: window.__POWERED_BY_QIANKUN__ ? '/app-vue/' : '/',
mode: 'history',
routes,
});
instance = new Vue({
router,
store,
render: (h) => h(App),
}).$mount(container ? container.querySelector('#app') : '#app');
// 將主應(yīng)用的函數(shù)掛到原生上方便調(diào)用
Vue.prototype.parentRouter = mainRouter
}
// 獨(dú)立運(yùn)行時(shí)
if (!window.__POWERED_BY_QIANKUN__) {
render();
}
export async function bootstrap() {
console.log('[vue] vue app bootstraped');
}
export async function mount(props) {
console.log('[vue] props from main framework', props);
render(props);
}
export async function unmount() {
instance.$destroy();
instance.$el.innerHTML = '';
instance = null;
router = null;
}以上就是微前端qiankun主應(yīng)用與子應(yīng)用之間的跳轉(zhuǎn)示例的詳細(xì)內(nèi)容,更多關(guān)于qiankun主應(yīng)用子應(yīng)用跳轉(zhuǎn)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue點(diǎn)擊右鍵出現(xiàn)自定義操作菜單實(shí)現(xiàn)代碼
這篇文章主要給大家介紹了關(guān)于vue點(diǎn)擊右鍵出現(xiàn)自定義操作菜單實(shí)現(xiàn)的相關(guān)資料,在網(wǎng)頁中我們也希望可以像桌面軟件一樣,點(diǎn)擊右鍵后出現(xiàn)操作菜單,對(duì)選中的數(shù)據(jù)項(xiàng)進(jìn)行相應(yīng)的操作,需要的朋友可以參考下2023-08-08
vue簡(jiǎn)單封裝axios插件和接口的統(tǒng)一管理操作示例
這篇文章主要介紹了vue簡(jiǎn)單封裝axios插件和接口的統(tǒng)一管理操作,結(jié)合具體實(shí)例形式分析了vue中axios插件安裝、配置及接口統(tǒng)一管理具體操作技巧,需要的朋友可以參考下2020-02-02
echarts 使用formatter 修改鼠標(biāo)懸浮事件信息操作
這篇文章主要介紹了echarts 使用formatter 修改鼠標(biāo)懸浮事件信息操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07
Vue+Element樹形表格實(shí)現(xiàn)拖拽排序示例
本文主要介紹了Vue+Element樹形表格實(shí)現(xiàn)拖拽排序示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
vue 實(shí)現(xiàn)拖拽動(dòng)態(tài)生成組件的需求
這篇文章主要介紹了vue 如何實(shí)現(xiàn)拖拽動(dòng)態(tài)生成組件的需求,幫助大家更好的理解和學(xué)習(xí)使用vue框架,感興趣的朋友可以了解下2021-05-05
Element?Plus在el-form-item中設(shè)置justify-content無效的解決方案
這篇文章主要介紹了Element?Plus在el-form-item中設(shè)置justify-content無效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
vue3中的watch和watchEffect實(shí)例詳解
watch和watchEffect都是監(jiān)聽器,但在寫法和使用上有所區(qū)別,下面這篇文章主要給大家介紹了關(guān)于vue3中watch和watchEffect的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
Vue.js組件tree實(shí)現(xiàn)無限級(jí)樹形菜單
這篇文章主要為大家詳細(xì)介紹了Vue.js組件tree實(shí)現(xiàn)無限級(jí)樹形菜單代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12

