From 4856f06f41563b8fa2d12aaa1247d8925eb43aa3 Mon Sep 17 00:00:00 2001 From: chenghongxing <1126263215@qq.com> Date: Thu, 3 Sep 2020 18:47:03 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20configuration=20problem=20of=20first=20r?= =?UTF-8?q?oute's=20path;=20:bug:=20=E4=BF=AE=E5=A4=8D=EF=BC=9A=E4=B8=80?= =?UTF-8?q?=E7=BA=A7=E8=B7=AF=E7=94=B1=20path=20=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router/index.js | 4 ++-- src/utils/routerUtil.js | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/router/index.js b/src/router/index.js index 3bdcc3c..c5aa144 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -1,6 +1,6 @@ import Vue from 'vue' import Router from 'vue-router' -import {formatAuthority} from '@/utils/routerUtil' +import {formatRoutes} from '@/utils/routerUtil' Vue.use(Router) @@ -25,7 +25,7 @@ const loginIgnore = { */ function initRouter(isAsync) { const options = isAsync ? require('./async/config.async').default : require('./config').default - formatAuthority(options.routes) + formatRoutes(options.routes) return new Router(options) } export {loginIgnore, initRouter} diff --git a/src/utils/routerUtil.js b/src/utils/routerUtil.js index dfab740..34a8941 100644 --- a/src/utils/routerUtil.js +++ b/src/utils/routerUtil.js @@ -66,7 +66,7 @@ function loadRoutes({router, store, i18n}, routesConfig) { if (asyncRoutes) { if (routesConfig && routesConfig.length > 0) { const routes = parseRoutes(routesConfig, routerMap) - formatAuthority(routes) + formatRoutes(routes) const finalRoutes = mergeRoutes(router.options.routes, routes) router.options = {...router.options, routes: finalRoutes} router.matcher = new Router({...router.options, routes:[]}).matcher @@ -134,6 +134,20 @@ function deepMergeRoutes(target, source) { return parseRoutesMap(merge) } +/** + * 格式化路由 + * @param routes 路由配置 + */ +function formatRoutes(routes) { + routes.forEach(route => { + const {path} = route + if (!path.startsWith('/') && path !== '*') { + route.path = '/' + path + } + }) + formatAuthority(routes) +} + /** * 格式化路由的权限配置 * @param routes 路由 @@ -203,4 +217,4 @@ function loadGuards(guards, options) { }) } -export {parseRoutes, loadRoutes, formatAuthority, getI18nKey, loadGuards, deepMergeRoutes} +export {parseRoutes, loadRoutes, formatAuthority, getI18nKey, loadGuards, deepMergeRoutes, formatRoutes}