简体   繁体   English

嵌套详细信息路由,反应路由器不呈现

[英]Nested details route with react-router not rendering

I am using the react-router plugin to setup my routes I want to provide two routes list/ and list/:id (pretty common) What I have and what's working is this: 我正在使用react-router插件设置我的路由我想提供两个路由list/list/:id (很常见)我有什么以及它的工作原理是这样的:

<Routes>
    <Route handler={App}>
        <Route name="list"
            handler={ListComponent}
            collection={new Collection()}>
        </Route>
    </Route>
</Routes>

But I am struggling to get my "details" route to work. 但我正努力让我的“细节”路线开始工作。 What I tried is: 我尝试的是:

<Routes>
    <Route handler={App}>
        <Route name="list"
            handler={ListComponent}
            collection={new Collection()}>
            <Route name="list.details"
                path="list/:id"
                handler={DetailsComponent}>
        </Route>
    </Route>
</Routes>

First, this is not working currently, my DetailsComponent is not rendered when accessing list/123 (eg) 首先,这当前不起作用,访问list / 123时不会呈现我的DetailsComponent (例如)

Second, even if this would work: how would I manager to pass one model in my collection "down" to the DetailsComponent ? 其次,即使这样可行:我如何管理我的集合中的一个模型“向下”传递给DetailsComponent

To answer your first question, there are two issues with your router code. 要回答您的第一个问题,您的路由器代码有两个问题。 Coincidentally, a forward slash is the answer to both issues: 巧合的是,正斜杠是这两个问题的答案:

  1. You need to close all your Route components. 您需要关闭所有Route组件。 If you write a <Route> by itself, you must include the closing tag like this: <Route/> . 如果您自己编写<Route> ,则必须包含结束标记,如下所示: <Route/>

  2. You likely need a forward slash at the start of your list path. 您可能需要在列表路径的开头使用正斜杠。

So your code should look something like: 所以你的代码应该是这样的:

<Routes>
    <Route handler={App}>
        <Route name="list"
            handler={ListComponent}
            collection={new Collection()}>
            <Route name="listDetails"
                path="/list/:id"
                handler={DetailsComponent}/>
        </Route>
    </Route>
</Routes>

Also, just to be clear, the route name is just a string identifier. 另外,为了清楚起见,路由名称只是一个字符串标识符。 I'm not sure if you wrote list.details as an object accessor to get to your model, but if so, it's not going to do what you think it will. 我不确定你是否将list.details写为对象访问器来获取你的模型,但如果是这样,它就不会按照你的想法去做。 I changed the name to listDetails to avoid any confusion, but list.details is also a valid string identifier so feel free to change it back if you'd like. 我将名称更改为listDetails以避免任何混淆,但list.details也是一个有效的字符串标识符,所以如果您愿意,可以随意更改它。

That resolves the code issues in the block you provided here. 这解决了您在此处提供的块中的代码问题。 Without seeing the DetailsComponent component and/or any error messages, I can't say for sure if there are any other issues. 在没有看到DetailsComponent组件和/或任何错误消息的情况下,我无法确定是否存在任何其他问题。

Regarding your second query, the solution is fairly straightforward (although react-router does not have the Backbone integration that you might be hoping for here). 关于你的第二个查询,解决方案相当简单(尽管react-router没有你可能希望的Backbone集成)。 However, I'm afraid I'm only permitted to answer one question at a time according to StackOverflow rules. 但是,我担心我只能根据StackOverflow规则一次回答一个问题。 So I'd be happy to provide an answer if you create a separate question. 如果你创建一个单独的问题,我很乐意提供答案。

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

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