无法使用React Router获得嵌套路由以进行匹配

布兰登

我在获取嵌套路线以进行匹配时遇到了麻烦:

<Router history={browserHistory}>
  <Route path="/" component={App}>
    <IndexRoute component={Home} />
    <Route path="about" component={About} />
    <Route path="work" component={Work}>
      <Route path=":slug" component={Sample} />
    </Route>
  </Route>
</Router>

在使用此路由器的情况下,我无法匹配以下路由:/work/sample-1应用程序不会引发错误,我也无法在Sample该类上记录任何语句

即使我对要匹配的值进行了硬编码,也无法使用。如果我取消嵌套该路线,并将其设置为路径,work/:slug则可以按预期工作。

我究竟做错了什么?

布兰登

终于在React Router Github页面“ sidebar”示例中找到了我正在寻找的模式

我嵌套了路线:

<Router history={browserHistory}>
  <Route path="/" component={App}>
    <Route path="work" component={Work}>
      <Route path=":slug" component={Sample} />
    </Route>
  </Route>
</Router>

然后render,在Work组件方法中,我只需要显示路线的子代或组件的其余部分:

render() {
    <div>
        {this.props.children ||
        <div>
            {/* rest of "Work" component here */}
        </div>}
    </div>
}

现在,当我导航到嵌套路线时,将选择我的导航项,并显示嵌套内容。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章