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