简体   繁体   English

多动态路由 (react-router-dom)

[英]Multi Dynamic Routes (react-router-dom)

i have two different dynamic routes, why second dynamic route doesn't work (its return Activities component it should be Hotspots ) thanks in advance我有两个不同的动态路由,为什么第二个动态路由不起作用(它的返回活动组件应该是 Hotspots )提前致谢

 <Switch>
        <Route exact path ='/' component={Home} />
        <Route path={`/:City/:CatName`} component={Activities} />
        <Route  path={`/:City/Hotspots`} component={Hotspots} />
      </Switch>

here is Links这是链接

<li>
                    <span><Link to={`/${Order.City.replace(' ', '-')}/Hotspots`}>Hotspots</Link></span>
                </li>
                <li>
                    <span><Link to={`/${Order.City.replace(' ', '-')}/Activities`}>Activities</Link></span>
                </li>

That's because url City/Hotspots is getting in Activities route.那是因为 url City/Hotspots正在进入Activities路线。

As you are setting Activities route with :CatName it indicates that this part of the URL is a placeholder, so when your URL are like /:City/Hotspots its understand the Hotspots is as placeholder for :CatName .当您使用:CatName设置活动路由时,它表明 URL 的这一部分是占位符,因此当您的 URL 类似于/:City/Hotspots它会理解 Hotspots 作为:CatName占位符。

To fix it just change the route's order, leaving Hotspots route as first, like:要修复它,只需更改路线的顺序,将Hotspots路线保留为第一,例如:

<Route path={`/:City/Hotspots`} component={Hotspots} />
<Route path={`/:City/:CatName`} component={Activities} />

So, now when you entered /:City/Hotspots , the first route that react-router-dom find will be the right one, and any other path with /:City/ will go to Activities route.因此,现在当您输入/:City/Hotspots , react-router-dom 找到的第一条路线将是正确的,任何其他带有/:City/路径都将转到Activities路线。

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

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