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}