提交 d351832c authored 作者: vipcxj's avatar vipcxj

fix: 支持多级路由

上级 19996b0b
...@@ -7,7 +7,7 @@ const Empty = ({ children }) => { ...@@ -7,7 +7,7 @@ const Empty = ({ children }) => {
}; };
const parsePageToRoute = (page, componentMapper) => { const parsePageToRoute = (page, componentMapper) => {
const { pages, childRoutes, component, ...route } = page; const { pages, childRoutes, component, entry, indexRoute, ...route } = page;
if (!component) { if (!component) {
route.component = Empty; route.component = Empty;
} else { } else {
...@@ -21,6 +21,24 @@ const parsePageToRoute = (page, componentMapper) => { ...@@ -21,6 +21,24 @@ const parsePageToRoute = (page, componentMapper) => {
if (thePages && thePages.length > 0) { if (thePages && thePages.length > 0) {
route.childRoutes = thePages.map(p => parsePageToRoute(p, componentMapper)); route.childRoutes = thePages.map(p => parsePageToRoute(p, componentMapper));
} }
const theIndexRoute = entry || indexRoute;
if (theIndexRoute) {
if (typeof theIndexRoute !== 'object') {
throw new Error('The \'entry\' must be an object with struct: { path: string, dsName: string, params: object }.');
}
const { path: entryPath, ...state } = theIndexRoute;
if (!entryPath || findRoutesByPath(route.childRoutes, entryPath).length === 0) {
throw new Error('The \'entry\' should have a valid path property.');
}
route.indexRoute = {
onEnter(nextState, replace) {
const theState = nextState.state || {};
replace(makePath(nextState.match.url, entryPath), { ...theState, ...state });
},
};
} else if (route.childRoutes && route.childRoutes.length > 0) {
throw new Error('No entry is found!');
}
return route; return route;
}; };
...@@ -48,7 +66,7 @@ const findRoutesByPath = (routes, path) => { ...@@ -48,7 +66,7 @@ const findRoutesByPath = (routes, path) => {
}; };
const _findRouteByPath = (route, parts) => { const _findRouteByPath = (route, parts) => {
if (parts.length === 0) { if (!route || parts.length === 0) {
return []; return [];
} }
const [current, ...others] = parts; const [current, ...others] = parts;
...@@ -71,6 +89,9 @@ const _findRouteByPath = (route, parts) => { ...@@ -71,6 +89,9 @@ const _findRouteByPath = (route, parts) => {
}; };
const _findRoutesByPath = (routes, parts) => { const _findRoutesByPath = (routes, parts) => {
if (!routes) {
return [];
}
for (const route of routes) { for (const route of routes) {
const res = _findRouteByPath(route, parts); const res = _findRouteByPath(route, parts);
if (res.length > 0) { if (res.length > 0) {
...@@ -89,7 +110,7 @@ export const parseLayout = (layout, componentMapper) => { ...@@ -89,7 +110,7 @@ export const parseLayout = (layout, componentMapper) => {
} }
if (layout.entry) { if (layout.entry) {
if (typeof layout.entry !== 'object') { if (typeof layout.entry !== 'object') {
throw new Error('The \'entry\' must be an object with struct: { path: string, datasource: string, params: object }.'); throw new Error('The \'entry\' must be an object with struct: { path: string, dsName: string, params: object }.');
} }
const { path: entryPath, ...state } = layout.entry; const { path: entryPath, ...state } = layout.entry;
if (!entryPath || findRoutesByPath(route.childRoutes, entryPath).length === 0) { if (!entryPath || findRoutesByPath(route.childRoutes, entryPath).length === 0) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论