简体   繁体   English

react-router中的相对路由

[英]relative routing in react-router

Did anyone know how to create for any path that ends with '/popup-image-:id' ? 有谁知道如何为以'/ popup-image-:id'结尾的路径创建文件? i have website where each image could be opened in popup on any page. 我有一个网站,可以在任何页面的弹出窗口中打开每个图像。 So, if i couldn't implement '/popup-image-:id' i have to double each route in my website to place inside. 因此,如果我无法实现'/ popup-image-:id',我必须将网站中的每条路线加倍以放置在内部。 I would like avoid redundance and fragility. 我想避免冗余和脆弱。 my current routing: 我目前的路线:

<Router history={browserHistory}>
   <Route path="/" component={Base}>
      <Route path="profile" component={Profile}>
         <Route path="popup-image-:id" component={Full_Piture} />
      </Route>
      <Route path="feed" component={Feed_List}>
         <Route path="popup-image-:id" component={Full_Piture} />
      </Route>
      <Route path="user-:id" component={User_Page}>
         <Route path="popup-image-:id" component={Full_Piture} />
      </Route>      
   </Route>
</Router>

As u can see i had to produce a lot of duplicated code. 如您所见,我不得不产生很多重复的代码。 If i could write something like <Route path="*/popup-image-:id" component={Full_Piture} /> it will be much cleaner and solid code 如果我可以编写类似<Route path="*/popup-image-:id" component={Full_Piture} />代码,它将使代码更<Route path="*/popup-image-:id" component={Full_Piture} />

Is the parent <Route> also supposed to match? <Route>也应该匹配? If it isn't, you should be able to use the ** syntax. 如果不是,则应该可以使用**语法。

<Route path="/**/popup-image-:id" component={Full_Picture} />

If the parent <Route> should also match, you could create a <RouteWithPopup> component that returns a <Route> which has the <Route path='popup-image-:id'> as its child. 如果父<Route>也应该匹配,则可以创建一个<RouteWithPopup>组件,该组件返回一个<Route> ,该组件具有<Route path='popup-image-:id'>作为其子代。

const RouteWithPopup = (props) => {
  return (
    <Route {...props}>
      <Route path="popup-image-:id" component={Full_Picture} />
    </Route>
  )
}

Then you could turn your route config into: 然后,您可以将路由配置转换为:

<Router history={browserHistory}>
  <Route path="/" component={Base}>
    <RouteWithPopup path="profile" component={Profile} />
    <RouteWithPopup path="feed" component={Feed_List} />
    <RouteWithPopup path="user-:id" component={User_Page} />
  </Route>
</Router>

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

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