2017-07-31 105 views
0

IndexRoute相当于反应路由器V4

<Router> 
 
    <div> 
 
    <Route exact path="/" component={App}/> 
 
    <Route exact path='/' component={Home}/> 
 
    <Route path="/Project" component={Project}/> 
 
    </div> 
 
</Router> 
 
)

家组件在应用组件是如何呈现为一个孩子(另类“IndexRoute”)

+0

的可能的复制[指定react-子路由路由器v4](https://stackoverflow.com/questions/44452858/specify-child-routes-in-react-router-v4) –

回答

1

最简单的答案只是移动Home到渲染()您的应用程序的方法,像这样:

class App extends Component { 
    render() { 
     return (
      <Home /> 
     ); 
    } 
} 

然后,在你的路由在顶端,你可能只是有这样的:

<Router> 
    <div> 
    <Route exact path="/" component={App}/> 
    <Route path="/Project" component={Project}/> 
    </div> 
</Router> 

但是,这完全取决于您想要做什么。作为儿童组件,App总是有Home吗?否则,你可以窝在路由器内的内部组件,如下所示:

这将设置应用程序的props.children是您要使用的特定组件。这可能您的应用程序内使用,然后像这样:

class App extends Component { 
    render() { 
     return (
      <MyHeaderComponent /> 
      {this.props.children} 
      <MyFooterComponent /> 
     ); 
    } 
} 
+0

是的应用程序始终有家作为一个孩子组件如何做到这一点 – None

-1

在下面的代码导航到/将呈现App组件与Home作为其子

<Route path="/" component={App}> 
    <IndexRoute component={Home}/> 
    <Route path="project" component={Project}/> 
</Route>