简体   繁体   English

子路由在React Router V4中不起作用

[英]Sub routes not working in react router v4

I'm trying to nest routes in react router but \\sports\\cricket sub-routes like this aren't showing up. 我正在尝试将路由嵌套在react路由器中,但是没有显示\\sports\\cricket子路由。 Following is the code: 以下是代码:

index.js index.js

const Root = () => {
  return (
    <Router>
      <div>
        <Route exact path="/" component={Home}/>
        <Route path="/sports" component={Sports} />
      </div>
    </Router>
  );
}; 

sports.js sports.js

return (
      <div>
        <Route path="cricket" component={Cricket} />
      </div>
    );

The console logs error 404 whenever I try to get /sports/cricket : 每当我尝试获取/sports/cricket时,控制台都会记录错误404:

在此处输入图片说明

Edit 1: Moved the nested route inside of the Sports component. 编辑1:将嵌套路线移至“ Sports组件内。

Nested routes don't work in v4. 嵌套路由在v4中不起作用。 Assign subroutes in the component that the parent route points to. 在父路线指向的组件中分配子路线。 Like this: 像这样:

index.js index.js

const Root = () => {
  return (
    <Router history={browserHistory}>
      <div>
        <Route exact path="/" component={Home}/>
        <Route path="/sports" component={Sports}/>
      </div>
    </Router>
  );
}; 

sports.js sports.js

return (
  <div>
    <Route path="/sports/cricket" component={Cricket} />
  </div>
);

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

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