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

fix: 支持多级路由

上级 19996b0b
......@@ -7,7 +7,7 @@ const Empty = ({ children }) => {
};
const parsePageToRoute = (page, componentMapper) => {
const { pages, childRoutes, component, ...route } = page;
const { pages, childRoutes, component, entry, indexRoute, ...route } = page;
if (!component) {
route.component = Empty;
} else {
......@@ -21,6 +21,24 @@ const parsePageToRoute = (page, componentMapper) => {
if (thePages && thePages.length > 0) {
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;
};
......@@ -48,7 +66,7 @@ const findRoutesByPath = (routes, path) => {
};
const _findRouteByPath = (route, parts) => {
if (parts.length === 0) {
if (!route || parts.length === 0) {
return [];
}
const [current, ...others] = parts;
......@@ -71,6 +89,9 @@ const _findRouteByPath = (route, parts) => {
};
const _findRoutesByPath = (routes, parts) => {
if (!routes) {
return [];
}
for (const route of routes) {
const res = _findRouteByPath(route, parts);
if (res.length > 0) {
......@@ -89,7 +110,7 @@ export const parseLayout = (layout, componentMapper) => {
}
if (layout.entry) {
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;
if (!entryPath || findRoutesByPath(route.childRoutes, entryPath).length === 0) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论