Commit 51e95274 authored by 邓晓峰's avatar 邓晓峰

fix: 修复renderRoutes methods

parent 293458fb
......@@ -2,9 +2,9 @@ import React, { Suspense } from 'react';
import { Helmet } from 'react-helmet';
import { connect } from 'react-redux';
import { renderRoutes } from 'react-router-config';
// import { RouteWithSubRoutes, renderRoutes} from '../../utils/routes'
import BootPage from '../../pages/bootpage';
// import { renderRoutes } from 'react-router-config';
import { renderRoutes } from '../../utils/routes'
// import BootPage from '../../pages/bootpage';
import {
Router,
Switch,
......@@ -13,7 +13,7 @@ import {
} from '@wisdom-utils/runtime';
import { dyRoutes } from '../../routes/config';
import routeContext from '@ant-design/pro-layout/lib/RouteContext';
// import routeContext from '@ant-design/pro-layout/lib/RouteContext';
// import { Route } from 'react-router-dom';
const pkg = require('../../../package.json');
......
......@@ -18,7 +18,8 @@ import {
Tooltip
} from 'antd';
// import { RouteWithSubRoutes, renderRoutes } from '../utils/routes';
import { renderRoutes } from 'react-router-config';
// import { renderRoutes } from 'react-router-config';
import { renderRoutes } from '../utils/routes'
import { PageContainer } from '@ant-design/pro-layout';
import Icon from '@ant-design/icons';
import { store } from 'microser-data';
......
......@@ -5,7 +5,7 @@ import {
HelmetProvider,
} from 'react-helmet-async';
import { connect } from 'react-redux';
import { renderRoutes } from 'react-router-config';
import { renderRoutes } from '../utils/routes';
import {
getMenuData,
......
import { transformURL } from './utils';
import {
Route,
Switch
} from '@wisdom-utils/runtime';
import { matchPath, Route, Switch} from 'react-router';
import React, { Suspense, Fragment } from 'react';
export const isURL = function(url) {
// eslint-disable-next-line no-useless-escape
......@@ -149,35 +147,64 @@ export function RouteWithSubRoutes(route) {
<Route path={route.path} render={props => (<route.component {...props} route={route} routes={route.routes} params={route.params}/>)}></Route>
)
}
export function renderRoutes(routes) {
return routes.map((route,index) => (
<Route
key={index}
path={route.path}
exact={route.exact}
render={(props) => { // 利用render 方法处理
if (route.routes){
return (
<>
<route.component {...props} route={route} routes={route.routes} params={route.params}></route.component>
<Switch>
{
route.routes.map((child,i) => (
RouteWithSubRoutes(child)
))
}
{/* <Redirect to={route.routes[0].path}></Redirect> // 子路由找不到,重定向到第一个子路由 */}
</Switch>
</>
)
}else {
return (
<route.component {...props} route={route} routes={route.routes} params={route.params}></route.component>
)
function matchRoutes(routes, pathname, branch) {
if (branch === void 0) {
branch = [];
}
routes.some(function (route) {
var match = route.path ? matchPath(pathname, route) : branch.length ? branch[branch.length - 1].match // use parent match
: Router.computeRootMatch(pathname); // use default "root" match
if (match) {
branch.push({
route: route,
match: match
});
if (route.routes) {
matchRoutes(route.routes, pathname, branch);
}
}}
/>
))
}
return match;
});
return branch;
}
function renderRoutes(routes, extraProps, switchProps) {
if (extraProps === void 0) {
extraProps = {};
}
if (switchProps === void 0) {
switchProps = {};
}
return routes ? React.createElement(Switch, switchProps, routes.map(function (route, i) {
return React.createElement(Route, {
key: route.key || i,
path: route.path,
exact: route.exact,
strict: route.strict,
render: function render(props) {
return route.render ? route.render(Object.assign({}, props, {}, extraProps, {
routes: route.routes,
params: route.params,
route: route
})) : React.createElement(route.component, Object.assign({}, props, extraProps, {
route: route,
routes: route.routes,
params: route.params,
}));
}
});
})) : null;
}
export {
renderRoutes,
matchPath
}
export default generRotes;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment