Skip to content

Commit

Permalink
feat: 获取首个子应用
Browse files Browse the repository at this point in the history
  • Loading branch information
dL-hx committed Aug 30, 2022
1 parent e6a2f93 commit af83143
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 5 deletions.
6 changes: 4 additions & 2 deletions main/micro/router/routerHandle.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { isTurnChild } from "../utils";
// 每次路由切换打印事件
export const turnApp = ()=>{
console.log('路由切换了');

if (isTurnChild()) {
console.log('路由切换了');
}
}
28 changes: 26 additions & 2 deletions main/micro/start.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// start 文件
import { setList } from './const/subApps'
import { setList ,getList } from './const/subApps'
// 实现路由拦截
import { rewriteRouter } from './router/rewriteRouter'

import { currentApp } from './utils'

rewriteRouter()

const registerMicroApps = (appList)=>{
Expand All @@ -11,7 +13,29 @@ const registerMicroApps = (appList)=>{
setList(appList)
}

// 启动微前端框架
const start = ()=>{
// 1. 获取当前子应用列表是否为空
const apps = getList()
if (!apps.length) {
// 子应用列表为空
throw Error('子应用列表为空, 请正确注册')
}
// 2. 有子应用内容,获取当前路由子应用
const app = currentApp()

if (app) {
const { pathname, hash } = window.location
const url = pathname + hash
window.history.pushState('', '', url)

window.__CURRENT_SUB_APP__ = app.activeRule
}

}


export default {
registerMicroApps
registerMicroApps,
start
}
32 changes: 32 additions & 0 deletions main/micro/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,40 @@
import { getList } from "../const/subApps"

// 给当前路由跳转打补丁
export const patchRouter = (globalEvent, eventName)=>{
return function(){
const e = new Event(eventName)
globalEvent.apply(this, arguments)
window.dispatchEvent(e)
}
}

// 获取当前子应用
export const currentApp = ()=>{
const currentUrl = window.location.pathname
return filterApp('activeRule', currentUrl)
}


// 过滤当前路由
const filterApp = (key, value)=>{// 当前key值===value值

const currentApp = getList().filter(item => item[key]===getCurrentPrefix()) // => array
// console.log('currentApp', currentApp);

return currentApp && currentApp.length ? currentApp[0]:{}
}

const getCurrentPrefix = (value=window.location.pathname)=>{
const currentPrefix = value.match(/(\/\w+)/g)
return currentPrefix[0]
}

// 子应用是否做了切换
export const isTurnChild = ()=>{

if (window.__CURRENT_SUB_APP__===getCurrentPrefix()) {
return false;
}
return true
}
1 change: 1 addition & 0 deletions main/src/const/nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const NAV_LIST = [
status: false,
value: 1,
url: '/react15/information',
hash:''
},
{
name: '视频',
Expand Down
5 changes: 4 additions & 1 deletion main/src/store/utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { MicroStart } from '../../micro'

const { registerMicroApps }= MicroStart
const { registerMicroApps, start }= MicroStart
// 注册子应用
export const registerApp=(list)=>{
// 注册到微前端框架
registerMicroApps(list)

// 启动微前端框架
start()
}

0 comments on commit af83143

Please sign in to comment.