she*_*mus 8 typescript reactjs react-router-v4
我的react应用程序有3个入口点,路径重叠,并且很难维护。基本上,其中两个应用只是停留在旧站点上的几个地方,直到主应用具有足以完全替换主站点的功能为止。
我正在使用React Router 4,并且所有路由都包含一个route.tsx文件。但是我想按功能对路由进行分组,然后让每个应用程序的路由组件都能满足需要。
目前,我的路线如下所示:
const MainAppRoutes: React.SFC = (): JSX.Element =>
{
return (
<Switch>
<Route exact path='/' component={HomePage} />
<Route path='/customers' component={CustomersDisplayPage} />
<Route path='/customers/:id' component={CustomersDisplayPage} />
<Route path='/cars' component={CarDisplayPage} />
<Route path='/cars/:id' component={CarDisplayPage} />
</Switch>
);
};
Run Code Online (Sandbox Code Playgroud)
但我希望它看起来像这样:
const MainAppRoutes: React.SFC = (): JSX.Element =>
{
return (
<Switch>
<Route exact path='/' component={HomePage} />
<CustomerAppRoutes />
<CarAppRoutes />
</Switch>
);
const CustomerAppRoutes: React.SFC = (): JSX.Element =>
{
return (
<Switch>
<Route path='/customers' component={CustomersDisplayPage} />
<Route path='/customers/:id' component={CustomersDisplayPage} />
</Switch>
);
};
const CarAppRoutes: React.SFC = (): JSX.Element =>
{
return (
<Switch>
<Route path='/cars' component={CarDisplayPage} />
<Route path='/cars/:id' component={CarDisplayPage} />
</Switch>
);
};
Run Code Online (Sandbox Code Playgroud)
但这会导致Caroutes无法正确路由。我尝试使用Div代替,但也不起作用。
小智 9
您可以将其放入单独的文件中,然后将它们映射到主文件中
客户路由.js
import ...
export default [
{ path: '/customers', component: CustomersDisplayPage },
{ path: '/customers/:id', component: CustomersDisplayPage }
]
Run Code Online (Sandbox Code Playgroud)
CarAppRoutes.js
import ...
export default [
{ path: '/cars', component: CarDisplayPage },
{ path: '/cars/:id', component: CarDisplayPage }
]
Run Code Online (Sandbox Code Playgroud)
主路由.js
import ...
import CarAppRoutes from 'wherever';
import CustomerRoutes from 'wherever';
...
<Switch>
<Route exact path='/' component={HomePage} />
{ CustomerRoutes.map(props => <Route {...props} />) }
{ CarAppRoutes.map(props => <Route {...props} />) }
</Switch>
Run Code Online (Sandbox Code Playgroud)
当我尝试使用 react-router-5 解决类似问题时,我发现了这个问题。Fragments 对我不起作用,实际上当我查看 Switch 组件实现时,我明白了原因。Fragment 仍然是一个反应组件,但 Switch 只希望 Routes 作为子组件。
就我而言,我可以使用一种解决方法。
我使用函数组件来返回 Route 数组。
我将函数组件称为函数而不是反应组件。
如果有必要,在 CustomerAppRoutes 和 CarAppRoutes 中有 props 没有问题。
const MainAppRoutes = () => (
<Switch>
<Route exact path='/' component={HomePage} />
{CustomerAppRoutes()}
{CarAppRoutes()}
</Switch>
);
const CustomerAppRoutes = () => ([
<Route path='/customers' component={CustomersDisplayPage} />,
<Route path='/customers/:id' component={CustomersDisplayPage} />
]);
const CarAppRoutes = () => ([
<Route path='/cars' component={CarDisplayPage} />,
<Route path='/cars/:id' component={CarDisplayPage} />
]);
Run Code Online (Sandbox Code Playgroud)
我当前使用的解决方案是创建路线数组:
//routes.tsx
import * as React from 'react';
export interface IRoute { path: string, component: React.ComponentClass, exact?: boolean }
const CarRoutes: IRoute[] = [
{ path: '/cars', component: {CarDisplayPage} },
{ path: '/cars/:id', component: {CarDisplayPage} },
];
const CustomerRoutes: IRoute[] = [
{ path: '/customers', component: {CustomerDisplayPage} },
{ path: '/customers/:id', component: {CustomerDisplayPage} },
];
const AppRouting: React.SFC = (): JSX.Element =>
{
const routes = []
.concat(CarRoutes)
.concat(CustomerRoutes);
return (
<Switch>
<Route exact path='/' component={HomePage} />
{routes.map(r => <Route path={r.path} exact={r.exact===true} path={r.path} key={r.path} />)}
</Switch>
);
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2891 次 |
| 最近记录: |