feat(router): Add dynamic routing

This commit is contained in:
陈凯龙
2022-01-11 10:47:10 +08:00
parent 95a2bd884d
commit b218ccc9cc
15 changed files with 478 additions and 323 deletions

View File

@@ -1,40 +1,40 @@
import router from './router'
import { useAppStoreWithOut } from '@/store/modules/app'
import { useCache } from '@/hooks/web/useCache'
// import type { RouteRecordRaw } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import { useTitle } from '@/hooks/web/useTitle'
import { useNProgress } from '@/hooks/web/useNProgress'
import { usePermissionStoreWithOut } from '@/store/modules/permission'
const permissionStore = usePermissionStoreWithOut()
const appStore = useAppStoreWithOut()
const { wsCache } = useCache()
const { toggle } = useNProgress()
const { start, done } = useNProgress()
const whiteList = ['/login'] // 不重定向白名单
router.beforeEach((to, from, next) => {
console.log(from)
toggle()
router.beforeEach(async (to, from, next) => {
start()
if (wsCache.get(appStore.getUserInfo)) {
if (to.path === '/login') {
next({ path: '/' })
} else {
// if (permissionStore.getIsAddRouters) {
// next()
// return
// }
// permissionStore.generateRoutes().then(() => {
// permissionStore.addRouters.forEach(async (route) => {
// await router.addRoute(route as RouteRecordRaw) // 动态添加可访问路由表
// })
// const redirectPath = from.query.redirect || to.path
// const redirect = decodeURIComponent(redirectPath as string)
// const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect }
// permissionStore.setIsAddRouters(true)
// next(nextData)
// })
next()
if (permissionStore.getIsAddRouters) {
next()
return
}
await permissionStore.generateRoutes()
permissionStore.getAddRouters.forEach((route) => {
router.addRoute(route as unknown as RouteRecordRaw) // 动态添加可访问路由表
})
const redirectPath = from.query.redirect || to.path
const redirect = decodeURIComponent(redirectPath as string)
const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect }
permissionStore.setIsAddRouters(true)
next(nextData)
}
} else {
if (whiteList.indexOf(to.path) !== -1) {
@@ -45,7 +45,7 @@ router.beforeEach((to, from, next) => {
}
})
router.afterEach(async (to) => {
router.afterEach((to) => {
useTitle(to?.meta?.title as string)
toggle() // 结束Progress
done() // 结束Progress
})