使用场景:本地全路由动态匹配线上路由
目的:本地路由方便开发配置,匹配线上路由,进行页面路由控制
内容:
export const constRoutes = [
{
path: '/',
redirect: '/home'
},
{
path: '/home',
name: 'home',
component: () => import('@/views/home/index.vue')
},
{
path: '/login',
name: 'login',
component: () => import('@/views/login/index.vue')
}
]
export const notFound = {
path: '/:pathMatch(.*)*',
name: 'notFound',
component: () => import('@/views/not-found/not-found.vue')
}
const Layout = () => import('@/layout/index.vue')
export const asyncRoutes = [
{
path: '/monitoringCenter',
name: 'MonitoringCenter',
component: Layout,
meta: { title: '监控中心' },
children: [
{
path: 'carMonitoring',
name: 'CarMonitoring',
meta: { title: '车辆监控' },
children: [
{
path: 'positioning',
name: 'Positioning',
component: () => import('@/views/monitoringCenter/positioning/index.vue'),
meta: { title: '实时定位' }
}
]
},
{
path: 'monitorSetting',
name: 'MonitorSetting',
meta: { title: '监控设置' },
children: [
{
path: 'ruleManagement',
name: 'RuleManagement',
component: () => import('@/views/monitoringCenter/ruleManagement/index.vue'),
meta: { title: '报警规则' }
}
]
}
]
},
import { createRouter, createWebHistory } from 'vue-router'
import { constRoutes } from '@/router/constRoutes'
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: constRoutes
})
export default router
import { asyncRoutes } from '@/router/asyncRoutes'
let redirect = ''
export function mapMenusToRoutes(menuList, Routes = asyncRoutes) {
const routes = []
for (let item of Routes) {
const m = menuList.find((menu) => item.name == menu.name)
if (m) {
m.attr && m.attr.length && (item.attr = m.attr)
item.id = m.id
if (item.children?.length) {
if (item.path.includes('/')) {
redirect = item.path
item.redirect = `${redirect}/${item.children[0].path}`
} else {
item.redirect = `${redirect}/${item.path}/${item.children[0].path}`
}
item.children = mapMenusToRoutes(m.children, item.children)
}
routes.push(item)
}
}
return routes
}
&&&&后面是具体调用方式,因人而异,可自行选择
import { defineStore } from 'pinia'
import router from '@/router'
import { notFound } from '@/router/constRoutes'
import localCache from '@/utils/cache'
import { mapMenusToRoutes } from '@/utils/map-menus'
import { login, getInfo } from '@/api/user/login'
export const useLoginStore = defineStore({
id: 'useLoginStore',
state: () => ({
token: '',
menuList: [],
routeMenus: []
}),
getters: {
routeMenusGet() {
return this.routeMenus
}
},
actions: {
async LoginAction(loginForm) {
const { data: loginData } = await login(loginForm)
this.token = loginData.token
localCache.setCache('token', this.token)
router.push('/home')
},
async getInfoAction() {
const infoData = await getInfo({ token: this.token })
this.menuList = infoData.data.menuList
this.addRouteMenus()
},
addRouteMenus() {
this.routeMenus = mapMenusToRoutes(this.menuList)
for (let item of this.routeMenus) {
router.addRoute(item)
}
router.addRoute(notFound)
}
}
})
import router from './router'
import localCache from '@/utils/cache'
import { useLoginStore } from '@/stores/user/login'
router.beforeEach(async (to, from, next) => {
document.title = `${to.meta.title}`
if (to.path === '/login') {
localCache.clearCache()
next()
} else {
const token = localCache.getCache('token')
if (!token) {
next(`/login`)
} else {
const loginStore = useLoginStore()
if (loginStore.routeMenus.length) {
next()
} else {
try {
await loginStore.getInfoAction()
next({ ...to, replace: true })
} catch (error) {
next(`/login`)
}
}
}
}
})
如有问题,请多指教