reactjs – 使用react-router-4的路由组
发布时间:2020-12-15 20:14:14 所属栏目:百科 来源:网络整理
导读:我的反应应用程序有3个重叠路径的入口点,并且很难维护.其中2个应用程序基本上只停留在旧网站的几个位置,直到主应用程序具有足够的功能来完全替换主站点. 我正在使用React路由器4,并有一个包含所有路由的routes.tsx文件.但我想按功能对路由进行分组,然后让每
我的反应应用程序有3个重叠路径的入口点,并且很难维护.其中2个应用程序基本上只停留在旧网站的几个位置,直到主应用程序具有足够的功能来完全替换主站点.
我正在使用React路由器4,并有一个包含所有路由的routes.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> ); }; 但我希望它看起来像这样: 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> ); }; 但这导致Caroutes不能正确路由.我尝试过使用Div’s,但这也不起作用. 解决方法
在React 16中,您可以使用
Fragments 完成此操作:
const MainAppRoutes: React.SFC = (): JSX.Element => ( <Switch> <Route exact path='/' component={HomePage} /> <CustomerAppRoutes /> <CarAppRoutes /> </Switch> ); const CustomerAppRoutes: React.SFC = (): JSX.Element => ( <> <Route path='/customers' component={CustomersDisplayPage} /> <Route path='/customers/:id' component={CustomersDisplayPage} /> </> ); const CarAppRoutes: React.SFC = (): JSX.Element => ( <> <Route path='/cars' component={CarDisplayPage} /> <Route path='/cars/:id' component={CarDisplayPage} /> </> ); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读