refactor(router/index.vue): 重置路由方法优化

This commit is contained in:
郝先瑞
2022-03-22 00:07:16 +08:00
parent 5f705f226d
commit 88ee9c7d3a

View File

@@ -1,13 +1,14 @@
import {createRouter, createWebHashHistory, RouteRecordRaw} from 'vue-router'
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
export const Layout = () => import( '@/layout/index.vue')
export const Layout = () => import('@/layout/index.vue')
// 参数说明: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
// 静态路由
export const constantRoutes: Array<RouteRecordRaw> = [
{
path: '/redirect',
component: Layout,
meta: {hidden: true},
meta: { hidden: true },
children: [
{
path: '/redirect/:path(.*)',
@@ -18,17 +19,17 @@ export const constantRoutes: Array<RouteRecordRaw> = [
{
path: '/login',
component: () => import('@/views/login/index.vue'),
meta: {hidden: true}
meta: { hidden: true }
},
{
path: '/404',
component: () => import('@/views/error-page/404.vue'),
meta: {hidden: true}
meta: { hidden: true }
},
{
path: '/401',
component: () => import('@/views/error-page/401.vue'),
meta: {hidden: true}
meta: { hidden: true }
},
{
path: '/',
@@ -39,11 +40,10 @@ export const constantRoutes: Array<RouteRecordRaw> = [
path: 'dashboard',
component: () => import('@/views/dashboard/index.vue'),
name: 'Dashboard',
meta: {title: 'dashboard', icon: 'dashboard', affix: true}
meta: { title: 'dashboard', icon: 'dashboard', affix: true }
}
]
},
// 外部链接
/*{
path: '/external-link',
@@ -95,16 +95,25 @@ export const constantRoutes: Array<RouteRecordRaw> = [
},
]
}*/
}
]
// 创建路由
const router = createRouter({
history: createWebHashHistory(),
routes: constantRoutes
routes: constantRoutes as RouteRecordRaw[],
// 刷新时,滚动条位置还原
scrollBehavior: () => ({ left: 0, top: 0 })
})
// 重置路由
export function resetRouter() {
const newRouter = router;
(router as any).matcher = (newRouter as any).matcher
router.getRoutes().forEach((route) => {
const name = route.name
if (name) {
router.hasRoute(name) && router.removeRoute(name)
}
})
}
export default router