提交 09a7b4e1 authored 作者: vipcxj's avatar vipcxj

feature: 模块中可以配置页面路由

上级 4c14d83c
...@@ -4,17 +4,21 @@ const route = (routes) => { ...@@ -4,17 +4,21 @@ const route = (routes) => {
const Wrapper = ({ children }) => { const Wrapper = ({ children }) => {
return children || null; return children || null;
}; };
Wrapper.route = { if (routes.indexRoute) {
indexRoute: { Wrapper.route = routes;
onEnter(nextState, replace) { } else {
if (routes && routes.childRoutes && routes.childRoutes.length > 0) { Wrapper.route = {
const index = routes.childRoutes[0]; indexRoute: {
replace(makePath(nextState.match.url, index.path)); onEnter(nextState, replace) {
} if (routes && routes.childRoutes && routes.childRoutes.length > 0) {
const index = routes.childRoutes[0];
replace(makePath(nextState.match.url, index.path));
}
},
}, },
}, ...routes,
...routes, };
}; }
return Wrapper; return Wrapper;
}; };
......
...@@ -20,12 +20,12 @@ const renderButton = (meta) => { ...@@ -20,12 +20,12 @@ const renderButton = (meta) => {
if (meta.path) { if (meta.path) {
const onClick = (e) => { const onClick = (e) => {
e.preventDefault(); e.preventDefault();
push(`../${meta.path}`, { ...meta }); push(meta.path, { ...meta });
}; };
const onKeyDown = (e) => { const onKeyDown = (e) => {
if (e.keyCode === 13) { if (e.keyCode === 13) {
e.preventDefault(); e.preventDefault();
push(`../${meta.path}`, { ...meta }); push(meta.path, { ...meta });
} }
}; };
// noinspection JSUnresolvedVariable // noinspection JSUnresolvedVariable
......
...@@ -118,8 +118,8 @@ const createRoutes = async (app, modules, groups, basePath) => { ...@@ -118,8 +118,8 @@ const createRoutes = async (app, modules, groups, basePath) => {
fullPath: combinePath(basePath, name), fullPath: combinePath(basePath, name),
name: showName, name: showName,
}; };
if (layout.route) { if (layout.route || layout.template) {
let routeBundle = await import(`./routes/main/modules/${layout.route}`); let routeBundle = await import(`./routes/main/modules/${layout.route || layout.template}`);
routeBundle = routeBundle.default || routeBundle; routeBundle = routeBundle.default || routeBundle;
const binder = bindModel(app, info, layout); const binder = bindModel(app, info, layout);
routeBundle = routeBundle(binder, info, layout); routeBundle = routeBundle(binder, info, layout);
......
/* eslint-disable dot-notation,no-param-reassign,no-underscore-dangle */
import { makePath } from './helper';
const Empty = ({ children }) => {
return children || null;
};
const parsePageToRoute = (page, componentMapper) => {
const { pages, childRoutes, component, ...route } = page;
if (!component) {
route.component = Empty;
} else {
const comp = componentMapper[component];
if (!comp) {
throw new Error(`Invalid component: ${component}.`);
}
route.component = comp;
}
const thePages = pages || childRoutes;
if (thePages && thePages.length > 0) {
route.childRoutes = thePages.map(p => parsePageToRoute(p, componentMapper));
}
};
const dealWithPath = (path) => {
if (!path || path === '/') {
return [];
}
let noStart = false;
let noEnd = false;
if (path.startsWith('/')) {
noStart = true;
}
if (path.endsWith('/')) {
noEnd = true;
}
if (noStart || noEnd) {
path = path.substring(noStart ? 1 : 0, noEnd ? (path.length - 1) : path.length);
}
return path.split('/');
};
const findRoutesByPath = (routes, path) => {
const parts = dealWithPath(path);
return _findRoutesByPath(routes, parts);
};
const _findRouteByPath = (route, parts) => {
if (parts.length === 0) {
return [];
}
const [current, ...others] = parts;
if (route.path === current) {
if (parts.length > 1) {
if (route.childRoutes) {
const res = _findRoutesByPath(route.childRoutes, others);
if (res.length > 0) {
return [route, ...res];
} else {
return [];
}
} else {
return [];
}
} else {
return [route];
}
}
};
const _findRoutesByPath = (routes, parts) => {
for (const route of routes) {
const res = _findRouteByPath(route, parts);
if (res.length > 0) {
return res;
}
}
return [];
};
export const parseLayout = (layout, componentMapper) => {
const route = {};
if (layout['pages'] || layout.childRoutes) {
route.childRoutes = (layout['pages'] || layout.childRoutes).map(page => parsePageToRoute(page, componentMapper));
} else {
throw new Error('No pages is found!');
}
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 }.');
}
const { path: entryPath, ...state } = layout.entry;
if (!entryPath || findRoutesByPath(route.childRoutes).length === 0) {
throw new Error('The \'entry\' should have a valid path property.');
}
route.indexRoute = {
onEnter(nextState, replace) {
replace(makePath(nextState.match.url, entryPath), state);
},
};
} else {
throw new Error('No entry is found!');
}
return route;
};
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论