使用React Router V4/V5的嵌套路由

编程入门 行业动态 更新时间:2024-10-27 12:39:23
本文介绍了使用React Router V4/V5的嵌套路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前正在使用React Router v4来嵌套路由.

I am currently struggling with nesting routes using react router v4.

最接近的示例是 React-Router v4文档.

我想将我的应用分为2个不同的部分.

I want to split my app in 2 different parts.

前端和管理区域.

我在想这样的事情:

<Match pattern="/" component={Frontpage}> <Match pattern="/home" component={HomePage} /> <Match pattern="/about" component={AboutPage} /> </Match> <Match pattern="/admin" component={Backend}> <Match pattern="/home" component={Dashboard} /> <Match pattern="/users" component={UserPage} /> </Match> <Miss component={NotFoundPage} />

前端的布局和样式与管理区域不同.因此在首页中,回家的路线大约应该是子路线.

The frontend has a different layout and style than the admin area. So within the frontpage the route home, about and so one should be the child routes.

/home 应该呈现到Frontpage组件中,而/admin/home 应该呈现在Backend组件中.

/home should be rendered into the Frontpage component and /admin/home should be rendered within the Backend component.

我尝试了一些变化,但是我总是以不打/home或/admin/home结尾.

I tried some variations but I always ended in not hitting /home or /admin/home.

编辑-2017年4月19日

因为此帖子现在有很多意见,所以我用最终解决方案对其进行了更新.我希望它能对某人有所帮助.

Because this post has a lot of views right now I updated it with the final solution. I hope it helps someone.

编辑-2017年8月5日

删除了旧的解决方案

最终解决方案:

这是我现在正在使用的最终解决方案.此示例还具有全局错误组件,例如传统的404页面.

This is the final solution I am using right now. This example also has a global error component like a traditional 404 page.

import React, { Component } from 'react'; import { Switch, Route, Redirect, Link } from 'react-router-dom'; const Home = () => <div><h1>Home</h1></div>; const User = () => <div><h1>User</h1></div>; const Error = () => <div><h1>Error</h1></div> const Frontend = props => { console.log('Frontend'); return ( <div> <h2>Frontend</h2> <p><Link to="/">Root</Link></p> <p><Link to="/user">User</Link></p> <p><Link to="/admin">Backend</Link></p> <p><Link to="/the-route-is-swiggity-swoute">Swiggity swooty</Link></p> <Switch> <Route exact path='/' component={Home}/> <Route path='/user' component={User}/> <Redirect to={{ state: { error: true } }} /> </Switch> <footer>Bottom</footer> </div> ); } const Backend = props => { console.log('Backend'); return ( <div> <h2>Backend</h2> <p><Link to="/admin">Root</Link></p> <p><Link to="/admin/user">User</Link></p> <p><Link to="/">Frontend</Link></p> <p><Link to="/admin/the-route-is-swiggity-swoute">Swiggity swooty</Link></p> <Switch> <Route exact path='/admin' component={Home}/> <Route path='/admin/user' component={User}/> <Redirect to={{ state: { error: true } }} /> </Switch> <footer>Bottom</footer> </div> ); } class GlobalErrorSwitch extends Component { previousLocation = this.props.location componentWillUpdate(nextProps) { const { location } = this.props; if (nextProps.history.action !== 'POP' && (!location.state || !location.state.error)) { this.previousLocation = this.props.location }; } render() { const { location } = this.props; const isError = !!( location.state && location.state.error && this.previousLocation !== location // not initial render ) return ( <div> { isError ? <Route component={Error} /> : <Switch location={isError ? this.previousLocation : location}> <Route path="/admin" component={Backend} /> <Route path="/" component={Frontend} /> </Switch>} </div> ) } } class App extends Component { render() { return <Route component={GlobalErrorSwitch} /> } } export default App;

推荐答案

在react-router-v4中,您不嵌套<Routes />.而是将它们放在另一个<Component />中.

In react-router-v4 you don't nest <Routes />. Instead, you put them inside another <Component />.

例如

<Route path='/topics' component={Topics}> <Route path='/topics/:topicId' component={Topic} /> </Route>

应该成为

<Route path='/topics' component={Topics} />

使用

const Topics = ({ match }) => ( <div> <h2>Topics</h2> <Link to={`${match.url}/exampleTopicId`}> Example topic </Link> <Route path={`${match.path}/:topicId`} component={Topic}/> </div> )

这里是基本示例,直接来自react-router 文档.

Here is a basic example straight from the react-router documentation.

更多推荐

使用React Router V4/V5的嵌套路由

本文发布于:2023-11-02 18:03:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1553048.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   路由   Router   React

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!