微前端qiankun主應用與子應用之間的跳轉(zhuǎn)示例
具體需求
一個后臺管理系統(tǒng),子應用中的token時效后,接口請求報錯,這時候需要跳轉(zhuǎn)到主應用中的登錄頁面。
傳遞一個登錄方法
在主應用調(diào)用子應用
傳遞一個登錄方法,在有需要的地方調(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('加載子應用前,加載進度條=', 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('加載子應用前,進度條加載完成', app.name)
return Promise.resolve()
}
} );
// 啟動 qiankun
start();子應用接收主應用傳遞的參數(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');
// 將主應用的函數(shù)掛到原生上方便調(diào)用
Vue.prototype.$baseReRegister = reRegister
}
// 獨立運行時
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()方式進行跳轉(zhuǎn)
window.history.pushState({
user: {}
}, '', '/login')}
主應用開啟qiankun并向子應用傳遞數(shù)據(jù)
將主應用的路由在吊起子應用時傳遞過去,使用主應用的路由完成跳轉(zhuǎn)。但是嘗試未能成功,有采用這條思路做對的小伙伴可以給個建議。
主應用開啟qiankun并向子應用傳遞數(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('加載子應用前,加載進度條=', app.name)
const data = {
token: 'admin',
}
app.props.data = data
// 向子應用傳遞路由
app.props.mainRouter = router
return Promise.resolve()
},
// qiankun 生命周期鉤子 - 掛載后
afterMount: (app) => {
console.log('加載子應用前,進度條加載完成', app.name)
return Promise.resolve()
}
} );
// 啟動 qiankun
start();子應用接收數(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');
// 將主應用的函數(shù)掛到原生上方便調(diào)用
Vue.prototype.parentRouter = mainRouter
}
// 獨立運行時
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主應用與子應用之間的跳轉(zhuǎn)示例的詳細內(nèi)容,更多關(guān)于qiankun主應用子應用跳轉(zhuǎn)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue點擊右鍵出現(xiàn)自定義操作菜單實現(xiàn)代碼
這篇文章主要給大家介紹了關(guān)于vue點擊右鍵出現(xiàn)自定義操作菜單實現(xiàn)的相關(guān)資料,在網(wǎng)頁中我們也希望可以像桌面軟件一樣,點擊右鍵后出現(xiàn)操作菜單,對選中的數(shù)據(jù)項進行相應的操作,需要的朋友可以參考下2023-08-08
vue簡單封裝axios插件和接口的統(tǒng)一管理操作示例
這篇文章主要介紹了vue簡單封裝axios插件和接口的統(tǒng)一管理操作,結(jié)合具體實例形式分析了vue中axios插件安裝、配置及接口統(tǒng)一管理具體操作技巧,需要的朋友可以參考下2020-02-02
echarts 使用formatter 修改鼠標懸浮事件信息操作
這篇文章主要介紹了echarts 使用formatter 修改鼠標懸浮事件信息操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
Element?Plus在el-form-item中設(shè)置justify-content無效的解決方案
這篇文章主要介紹了Element?Plus在el-form-item中設(shè)置justify-content無效的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10

