有没有办法在React Router v4中嵌套路由?
这有效:
<Router basename='/app'> <main> <Route path='/' component={AppBar} /> <Route path='/customers' component={Customers} /> </main> </Router>
这不是:
<Router basename='/app'> <Route path='/' component={AppBar}> <Route path='/customers' component={Customers} /> </Route> </Router>
客户组成部分:
import React, { Component, PropTypes } from 'react' import styled from 'styled-components' export default class Customers extends Component { render () { return ( <Container> <h1>Customers</h1> </Container> ) } } const Container = styled.section` height: 100%; padding: 15px; overflow: auto; `
到目前为止,我发现的最佳模式。
// main app <div> // not setting a path prop, makes this always render <Route component={AppShell}/> <Switch> <Route exact path="/" component={Login}/> <Route path="/dashboard" component={AsyncDashboard(userAgent)}/> <Route component={NoMatch}/> </Switch> </div>
我可以继续将其嵌套在组件中,并且一切都很好,包括hmr(如果使用webpack,请不要忘记设置output.publicPath为"/")
output.publicPath
"/"
// dashboard component <div> // the same way as before, not setting a path prop // makes it render on every /dashboard/** request <Route component={DashboardTAB}/> <Switch> // longer path (with same root) than others first <Route path="/dashboard/graphs/longerpath" component={GraphForm}/> <Route path="/dashboard/graphs" component={Graphs}/> <Route path="/dashboard/workers" component={List}/> <Route path="/dashboard/insert" component={InsertComponent}/> </Switch> </div>