【发布时间】:2017-11-26 04:04:58
【问题描述】:
我正在尝试从反应路由器网站https://reacttraining.com/react-router 查找文档,但我似乎无法找到在 4.1.1 中使用嵌套路由的解决方案。例如,在旧版本的 React Router 中,您可以像这样嵌套 Routes:
<Router history={history}>
<Route exact path="/" component={App}>
<Route path="/cards" component={Example} />
</Route>
</Router>
这使用了 {this.props.children} 并使得在整个应用程序中存在诸如导航栏之类的静态组件变得容易,而无需在每个组件中导入它。
class App extends Component {
render() {
return (
<div>
<Header/>
<main>{ this.props.children }</main>
<Footer/>
</div>
);
}
}
如何在 4.1.1 版中出现类似的行为。我不想在组件中一直导入header组件。
我目前正在使用,但它似乎没有完成我想要做的事情。
<Router history={history}>
<Switch>
<Route exact path="/" component={App}>
<Route path="/cards" component={Example} />
</Route>
</Switch>
</Router>
【问题讨论】:
标签: reactjs react-router