From 88ee9c7d3a3c5940abbd93cf276222ffdf0f8c45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=9D=E5=85=88=E7=91=9E?= <1490493387@qq.com> Date: Tue, 22 Mar 2022 00:07:16 +0800 Subject: [PATCH] =?UTF-8?q?refactor(router/index.vue):=20=E9=87=8D?= =?UTF-8?q?=E7=BD=AE=E8=B7=AF=E7=94=B1=E6=96=B9=E6=B3=95=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router/index.ts | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/router/index.ts b/src/router/index.ts index 8fad429c..fa80bed7 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -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 = [ { path: '/redirect', component: Layout, - meta: {hidden: true}, + meta: { hidden: true }, children: [ { path: '/redirect/:path(.*)', @@ -18,17 +19,17 @@ export const constantRoutes: Array = [ { 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 = [ 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 = [ }, ] }*/ + } ] +// 创建路由 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 \ No newline at end of file