Skip to content

Commit

Permalink
feat:中央控制器-路由拦截
Browse files Browse the repository at this point in the history
  • Loading branch information
dL-hx committed Aug 28, 2022
1 parent 707f1ae commit e6a2f93
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
18 changes: 18 additions & 0 deletions main/micro/router/rewriteRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { patchRouter } from '../utils'
import { turnApp } from './routerHandle'

// 重写路由跳转: 实现路由拦截
export const rewriteRouter = () => {

window.history.pushState = patchRouter(window.history.pushState, 'micro_push')
window.history.replaceState = patchRouter(window.history.replaceState, 'micro_replace')

// add:event bind
window.addEventListener('micro_push', turnApp)
window.addEventListener('micro_replace', turnApp)

// 监听返回事件
window.onpopstate = function () {
turnApp()
}
}
5 changes: 5 additions & 0 deletions main/micro/router/routerHandle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// 每次路由切换打印事件
export const turnApp = ()=>{
console.log('路由切换了');

}
7 changes: 6 additions & 1 deletion main/micro/start.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { setList } from './const/subApps'
// start 文件
import { setList } from './const/subApps'
// 实现路由拦截
import { rewriteRouter } from './router/rewriteRouter'

rewriteRouter()

const registerMicroApps = (appList)=>{
// 注册到window上
// window.appList = appList
Expand Down
8 changes: 8 additions & 0 deletions main/micro/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// 给当前路由跳转打补丁
export const patchRouter = (globalEvent, eventName)=>{
return function(){
const e = new Event(eventName)
globalEvent.apply(this, arguments)
window.dispatchEvent(e)
}
}
3 changes: 2 additions & 1 deletion main/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"requireConfigFile": false
},
"rules": {
"no-unused-vars": 0
"no-unused-vars": 0,
"no-debugger": 0
}
},
"browserslist": [
Expand Down

0 comments on commit e6a2f93

Please sign in to comment.