react-router如何区分同级而不同参数的路由?

路由如下:

<Router history={history}>
    <Route path="/" component={App}>
        <IndexRoute component={Home} />
        <Route path=":category" component={Category} />
        <Route path=":postId.html" component={Post} />
        <Route path="*" component={NotFound} />
    </Route>
</ROuter>

这种情况怎么办呢?所有路由都被:category截掉了,进不了:postId.html路由。

阅读 4.2k
1 个回答

虽然我没怎么用过react-router,但也觉得你这样不OK。路由的配置重了,为了去重,最简单的方式就是加前缀:

<Router history={history}>
    <Route path="/" component={App}>
        <IndexRoute component={Home} />
        <Route path="category/:category" component={Category} />
        <Route path="post/:postId" component={Post} />
        <Route path="*" component={NotFound} />
    </Route>
</ROuter>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题