在React Router中我有一个嵌套的Route
<Route path='about' component={{main: About, header: Header}}>
<Route path='team' component={Team} />
</Route>
现在,当我转到/about/team
时,它显示了团队。
但是,当我访问/about
时,如何设置要查看的组件?
我试过了
<Route path='about' component={{main: About, header: Header}}>
<IndexRoute component={AboutIndex} />
<Route path='team' component={Team} />
</Route>
和
<Route path='about' component={{main: About, header: Header}}>
<Route path='/' component={AboutIndex} />
<Route path='team' component={Team} />
</Route>
但它没有用。
我的关于组件看起来像这样
class About extends React.Component {
render () {
return (
<div>
<div className='row'>
<div className='col-md-9'>
{this.props.children}
</div>
<div className='col-md-3'>
<ul className='nav nav-pills nav-stacked'>
<li className='nav-item'><IndexLink className='nav-link' to='/about' activeClassName='active'>About</IndexLink></li>
<li className='nav-item'><Link className='nav-link' to='/about/team'>Team</Link></li>
</ul>
</div>
</div>
</div>
);
}
}
答案 0 :(得分:2)
默认路线是没有路径的路线。
import BrowserRouter from 'react-router-dom/BrowserRouter';
import Switch from 'react-router-dom/Switch';
import Route from 'react-router-dom/Route';
<BrowserRouter>
<Switch>
<Route exact path='/about' component={AboutIndex} />
<Route component={AboutIndex} /> // <--- don't add a path for a default route
</Switch>
</BrowserRouter>
如果您在组件中不需要此对象{main: About, header: Header}
,则只需在组件属性中放置AboutIndex即可。这应该工作
<Router history={browserHistory}>
<Route path='about' component={AboutIndex}>
<IndexRoute component={AboutIndex} />
<Route path='team' component={Team} />
</Route>
</Router>
如果您仍然需要主要和标题组件,只需根据您的需要将它们添加为父组件,子组件或兄弟组件