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

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

View File

@@ -3,6 +3,7 @@ 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',
@@ -42,7 +43,6 @@ export const constantRoutes: Array<RouteRecordRaw> = [
meta: { title: 'dashboard', icon: 'dashboard', affix: true } meta: { title: 'dashboard', icon: 'dashboard', affix: true }
} }
] ]
},
// 外部链接 // 外部链接
/*{ /*{
@@ -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