【问题标题】:Warning: Location "/test/22" did not match any routes警告:位置“/test/22”不匹配任何路线
【发布时间】:2015-11-27 22:31:59
【问题描述】:

我在尝试使用 react 路由器创建嵌套路由时遇到问题。当我链接到第一条路线时一切正常,但任何其他嵌套都会给我一个Warning: Location "/test/22" did not match any routes 错误。

这是我的代码的 sn-ps,如果您需要更多信息,请告诉我。

当链接到/test/stop 时,下面的路由器工作正常,但是当尝试链接/test/22 时,它会发出错误。 Main.js下方

class AppRouter extends React.Component {
  render() {
    return (
      <Router history={history}>
        <Route path="/" component={App}>
          <Route path="stop" component={StopList}>
            <Route path=":stopid" component={Stop}/>
          </Route>
          <Route path="test" component={RouteList}>
            <Route path="/22" component={StopList}/>
          </Route>
          <Route path="findStops" component={FindStops}/>
        </Route>
      </Router>
    )
  }
}

使用测试第一个链接可以正常工作..不确定为什么第二个测试链接不起作用..findStops.js 下面

render() {
    return (
      <section>
          <h1>test page</h1>
          <Link to="/test">test first</Link><br />
          <Link to="/test/22" >test link</Link><br/>
      </section>
    )
  }

【问题讨论】:

    标签: javascript reactjs react-router


    【解决方案1】:

    如果有人有更好的答案,请告诉我,但我继续修改了这个

    <Route path="test" component={RouteList}>
       <Route path="/22" component={StopList}/>
    </Route>
    

    <Route path="test" component={RouteList} />
    <Route path="test/22" component={StopList}/>
    

    它有效,但感觉这可能不是正确的语法或不正确的样式,所以如果有不同的方式,请更正。我想首先做正确的事情。谢谢!

    【讨论】:

    • 我不得不做同样的事情来做类似的工作,我不明白为什么我必须在22之前再次包含test。最后你知道为什么了吗?好像子路由真的不起作用。
    【解决方案2】:

    您不需要嵌套路由的“/22”中的斜线,因此您应该将其更改为“22”。嵌套路由应该被假定为由斜线分隔。

    【讨论】:

      猜你喜欢
      • 2017-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-13
      • 1970-01-01
      • 1970-01-01
      • 2016-12-06
      • 1970-01-01
      相关资源
      最近更新 更多