在同一路线中使用路线组件和路线子项

时间:2019-01-15 16:43:48

标签: reactjs react-router

我是一个反应js的初学者,现在我正在练习路线,但遇到以下嵌套路线错误:

  

您不应在同一路线中使用“路线”组件和“路线”子级; <Route children>将被忽略。

在上面可以看到route和children组件不能在与我相同的路由中使用,所以我该如何修复下面的代码以使其起作用:

  const About=(props)=>(
   <div>
    {props.children}
    <h2>About</h2>
   </div>
  );


 const Cityimage=()=>(

        <h1>In City</h1>
 );

 const Sportsimage=()=>(
   <h2>In Sports</h2>
 );

ReactDOM.render(

<Router>
    <Switch>
        <Route path="/app" component={App} />

        <Route  path="/about" component={About}>
             <Route  path="city" component={Cityimage}/>
             <Route  path="sport" component={Sportsimage}/>
        </Route>

    </Switch>
</Router>
   ,document.getElementById('root'));

我有一条关于“父母”的路线,其两个孩子则是“城市和体育”路线。如何修复我的代码?

1 个答案:

答案 0 :(得分:0)

你可以尝试吗?

<Route  path="/about" component={About}/>
<Route  path="/about/city" component={Cityimage}/>
<Route  path="/about/sport" component={Sportsimage}/>

相关问题