简体   繁体   English

React-router.Nested路由

[英]React-router.Nested routes

How to make nested routes??? 如何制作嵌套路线???

I want initial route to be course/:course_id? 我希望初始路线为course /:course_id? after that, when i click on a node I want my url to become course/:course_id?/nodes/:node_id . 之后,当我单击一个节点时,我希望我的网址成为course /:course_id?/ nodes /:node_id。

I use : "react-router-dom": "^4.2.2" 我使用: “ react-router-dom”:“ ^ 4.2.2”

return (
    <Router>
      <div id="app-main">
        <Header />
        <Route path="/course/:course_id?" component = {Content}/>
            <Route path="/course/:course_id?/nodes" component = {Content}/>
        <Footer />
      </div>
    </Router>

    );

When i click id redirects me to course/nodes and skips :course_id 当我单击ID时,会将我重定向到课程/节点,并跳过:course_id

return(
        <div className="paragraph-text-child" onClick={() => this.props.select(chapter)} key={chapter.node_id} >
            <Link to="nodes">{chapter.text}</Link>
            {this.iterate(chapter.nodes)}
        </div>
    );

I think you have some concepts mixed up... Route is for handling received URLs, Link is for setting it. 我认为您混淆了一些概念...路由用于处理收到的URL,链接用于设置它。

Link does not know about Route, Route does not know about Link. 链接不知道路由,路由不知道链接。 Link sets the URL to what is specified in to . 链接设置的URL是什么规定to So if your current URL is /course, and to="nodes" , the result is /course/nodes. 因此,如果您当前的URL是/ course,并且是to="nodes" ,则结果是/ course / nodes。 If it was to="0/nodes" , the result would be /courses/0/nodes. 如果它是to="0/nodes" ,则结果将是/ courses / 0 / nodes。

Now if I understood correctly, you always want a number between "/courses" and "/nodes", correct? 现在,如果我理解正确,那么您总是希望在“ / courses”和“ / nodes”之间有一个数字,对吗?

This can be achieved with Redirect , which comes from react-router-dom too. 这也可以通过Redirect来实现, Redirect也来自react-router-dom

If you create the following Route: 如果创建以下路线:

<Route path="/courses" render={()=> <Redirect to="/courses/0"/>}/>

And rework the previous route so that course_id is NOT optional 并重做之前的路线,以便course_id并非可选

<Route path="/course/:course_id" component={Content}/>

When you navigate to /courses, you will be silently redirected to courses/0. 当您导航到/ courses时,您将被静默重定向到Courses / 0。 The result is that your Link component with to="nodes" will always redirect to courses/number/nodes - because effectively the courses/ location won't be reachable anymore. 结果是,带有to="nodes" Link组件将始终重定向到课程/编号/节点-因为实际上无法再访问课程/位置。 Every URL that would not contain a course_id, will be redirected to course_id = 0 每个不包含course_id的URL将被重定向到course_id = 0

Note that these 2 routes should be put in a Switch , and in the correct order, otherwise you will end up redirecting every time... 请注意,这2条路由应以正确的顺序放入Switch ,否则每次都将导致重定向...

I have not tested this, but it should do the job: 我尚未对此进行测试,但它应该可以完成此工作:

...
<Header/>
  <Switch>
    <Route path="/course/:course_id" component={Content}/>
    <Route path="/courses" render={()=> <Redirect to="/courses/0"/>}/>
  </Switch>
<Footer/>
...

And this should handle /course/course_id 这应该处理/ course / course_id

Now, if you want to nest a /course/:course_id/nodes/:node_id Route, that should go into the component rendered by the parent route. 现在,如果要嵌套/ course /:course_id / nodes /:node_id路由,则应将其放入父路由所呈现的组件中。

Let's rework our parent Route into this: 让我们将父路由修改为:

<Route path="/course/:course_id" render={(props) => <Content ...props />}/>

What this does is, instead of just rendering the passed component, it renders the component and passed down Router props. 这不仅是渲染通过的组件,还渲染了组件并向下传递了Router道具。 Which means that the rendered component will be able to handle routes! 这意味着渲染的组件将能够处理路线!

Now, in the Content component: 现在,在内容组件中:

render() {
  return <Route path="/course/:course_id?/nodes/:node_id?" component={NodeContent}/>
}

The last thing we need to do is change the to property of the Link component so that it will redirect to the target node: 我们需要做的最后一件事是更改Link组件的to属性,以便它将重定向到目标节点:

<Link to={"nodes/" + chapter.node_id}/>

Does this make sense? 这有意义吗? I may have missed some gotchas in your code - the idea of what you want to achieve is in here, but you may have to adapt it a bit... 我可能已经在您的代码中错过了一些陷阱-您想要实现的目标就在这里,但是您可能需要对其进行一些调整...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM