我所拥有的是root route
定义:
const rootRoute = {
childRoutes: [{
path: '/',
component: require('./screens/Container'),
appGlobalVars: appGlobalVars,
childRoutes: [
require('./routes/ListApps'),
require('./routes/Settings'),
require('./routes/Profile')
]
}]
};
var runApp = (appGlobalVars) => {
var routes = (
<Provider store={store}>
<Router history={ history } routes={rootRoute} />
</Provider>
);
render(routes, document.getElementById('app'));
};
以及嵌套动态路由的一些设置:
./路由/设置/ index.js:
module.exports = {
path: 'settings',
getComponent(nextState, cb) {
require.ensure([], (require) => {
cb(null, require('../../screens/AppSettings'))
})
},
getChildRoutes(partialNextState, cb) {
require.ensure([], (require) => {
cb(null, [
require('./General'),
require('./Users')
])
})
},
};
/settings
是父组件,它使{this.props.children}
反应路由器传递。例如,如果我导航到/settings/general
,我会settings
呈现,这反过来会将general
呈现为其子项:
./路由/设置/ General.js
module.exports = {
path: 'general',
getComponent(nextState, cb) {
require.ensure([], (require) => {
cb(null, require('../../screens/AppSettings/General'))
})
}
};
这没关系,但我想要做的是定义导航到Settings
时应呈现的默认子组件/settings
。
简而言之:在使用动态路由时,有没有办法定义类似IndexRoute
的内容?
答案 0 :(得分:1)
您应该使用getIndexRoute
- https://github.com/reactjs/react-router/blob/master/docs/API.md#getindexroutepartialnextstate-callback
答案 1 :(得分:0)
由于不推荐使用方法getIndexRoutes,我注意到indexRoute需要一个对象{component:SomeCompoent},它是getComponent返回的对象的模式,所以我使用getComponent来提供indexRoute,如下所示:
export default (store) => ({
path : 'shops',
childRoutes: [
EditShopRoute(store),
],
component: EntityLayout, // this renders always
indexRoute: { // this renders only when route matches exactly /shops
getComponent (nextState, cb) {
require.ensure([], (require) => {
const Shops = require('./containers/ShopsContainer').default
const reducerShop = require('./modules/shops').default
injectReducer(store, { key: 'shops', reducer: reducerShop })
cb(null, Shops)
/* Webpack named bundle */
}, 'shops')
} }
})