我会想象你的路由树是这样的:
/sites
(Sites.js)
- /:id1
(Site1.js)
- /:id2
(Site2.js)
您index.js里面,这样的事情(使用客户端为例):
render((
<Router history={browserHistory}>
<Route path="/" component={App}>
<IndexRoute component={Home} />
<Route path="/sites" component={Sites}>
<Route path="/sites/:id" component={Site}/>
</Route>
</Route>
</Router>
), document.getElementById('app'));
每条路都有附随零件。你可以在这些组件中抛出任何你想要的东西,尽管包含一些导航到子路由的方式可能会有所帮助。
PlainRoute版本(虽然我不知道你为什么会想):
const rootRoute = {
component: 'div',
childRoutes: [{
path: '/',
component: require('./components/App'),
childRoutes: [ require('./routes/Sites') ]
}]
}
render(
<Router history={browserHistory} routes={rootRoute} />,
document.getElementById('example')
);
'./routes/Sites ':
module.exports = {
path: 'sites',
getChildRoutes(location, cb) {
require.ensure([], (require) => cb(null, [ require('./routes/Site') ]))
},
getComponent(nextState, cb) {
require.ensure([], (require) => cb(null, require('./components/Sites')))
}
}
' ./routes/Site' :
module.exports = {
path: 'sites/:id',
getComponent(nextState, cb) {
require.ensure([], (require) => cb(null, require('./components/Site')))
}
}
您如何对PlainRoutes进行此操作,示例如何? – GreeKatrina
在可读性方面,当JSX更高效时,我不知道为什么要使用PlainRoutes。根据示例中使用的文件结构,我将在PlainRoutes版本中追加到最初的答案。 –
正如你可以看到[这里](https://github.com/reactjs/react-router/blob/master/examples/huge-apps/app.js),它更适合代码分割。无论如何,先谢谢你。 – GreeKatrina