Browse Source

路由优化

master
peng.peng 2 years ago
parent
commit
dd5d2b7ef8
  1. 29
      web/client/src/layout/index.js

29
web/client/src/layout/index.js

@ -31,37 +31,38 @@ const Root = props => {
function flat(routes, parentRoute) { function flat(routes, parentRoute) {
routes.forEach((route, i) => { routes.forEach((route, i) => {
let obj = { const obj = {
path: route.path, path: route.path,
breadcrumb: route.breadcrumb, breadcrumb: route.breadcrumb,
component: route.component || null, component: route.component || null,
authCode: route.authCode || '', authCode: route.authCode || '',
key: route.key key: route.key,
} };
if (!route.path.startsWith("/")) { if (!route.path.startsWith('/')) {
console.error('路由配置需以 "/" 开始:' + route.path); console.error(`路由配置需以 "/" 开始:${route.path}`);
} }
if (route.path.length > 1 && route.path[route.path.length] == '/') { if (route.path.length > 1 && route.path[route.path.length] == '/') {
console.error('除根路由路由配置不可以以 "/" 结束:' + route.path); console.error(`除根路由路由配置不可以以 "/" 结束:${route.path}`);
} }
if (parentRoute && parentRoute != '/') { if (parentRoute && parentRoute != '/') {
obj.path = parentRoute + route.path; obj.path = parentRoute + route.path;
} }
if (route.exact) { if (route.exact === false) {
obj.exact = true obj.exact = false;
} }
if (route.hasOwnProperty('childRoutes')) { if (route.hasOwnProperty('childRoutes')) {
combineRoutes.push(obj); combineRoutes.push(obj);
flat(route.childRoutes, obj.path) flat(route.childRoutes, obj.path);
} else { } else {
combineRoutes.push(obj) combineRoutes.push(obj);
} }
}) });
} }
flat(routes); flat(routes);
return combineRoutes; return combineRoutes;
} };
const initReducer = (reducers, reducerName, action) => { const initReducer = (reducers, reducerName, action) => {
let reducerParams = {} let reducerParams = {}
@ -90,7 +91,7 @@ const Root = props => {
let actions = { let actions = {
layout: layoutActions layout: layoutActions
} }
for (let s of sections) { for (let s of sections) {
if (!s.key) console.warn('请给你的section添加一个key值,section name:' + s.name); if (!s.key) console.warn('请给你的section添加一个key值,section name:' + s.name);
for (let r of s.routes) { for (let r of s.routes) {
@ -141,7 +142,7 @@ const Root = props => {
setCombineRoutes(combineRoutes.map(route => ( setCombineRoutes(combineRoutes.map(route => (
<Route <Route
key={route.key} key={route.key}
exact={route.hasOwnProperty('exact') ? route.exact : true} exact={Object.prototype.hasOwnProperty.call(route, 'exact') ? route.exact : true}
path={route.path} path={route.path}
component={route.component} component={route.component}
/> />

Loading…
Cancel
Save