You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.2 KiB

2 years ago
import type { RouteRecordRaw } from 'vue-router'
import type { App } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'
2 years ago
import { basicRoutes } from './routes'
// 白名单应该包含基本静态路由
const WHITE_NAME_LIST: string[] = []
function getRouteNames(array: any[]) {
return array.forEach((item) => {
2 years ago
WHITE_NAME_LIST.push(item.name)
getRouteNames(item.children || [])
})
}
2 years ago
getRouteNames(basicRoutes)
// app router
// 创建一个可以被 Vue 应用程序使用的路由实例
export const router = createRouter({
// 创建一个 hash 历史记录。
history: createWebHistory(import.meta.env.VITE_PUBLIC_PATH), // createWebHashHistory
2 years ago
// 应该添加到路由的初始路由列表。
routes: basicRoutes as unknown as RouteRecordRaw[],
// 是否应该禁止尾部斜杠。默认为假
strict: true,
scrollBehavior: () => ({ left: 0, top: 0 }),
2 years ago
})
// reset router
export function resetRouter() {
router.getRoutes().forEach((route) => {
const { name } = route
if (name && !WHITE_NAME_LIST.includes(name as string))
2 years ago
router.hasRoute(name) && router.removeRoute(name)
})
}
// config router
// 配置路由器
export function setupRouter(app: App<Element>) {
app.use(router)
}