使用动态路由时如何定义反应路由器的 IndexRoute
how to define a react router's IndexRoute when using dynamic routing
我有一个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'));
};
以及嵌套动态路由的一些设置:
./routes/Settings/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}
react router passes。例如,如果我导航到 /settings/general
,我将呈现 settings
,这又会呈现 general
作为其子项:
./routes/Settings/General.js
module.exports = {
path: 'general',
getComponent(nextState, cb) {
require.ensure([], (require) => {
cb(null, require('../../screens/AppSettings/General'))
})
}
};
这没问题,但我想做的是定义如果导航到 /settings
.
应该呈现的默认子组件 Settings
简而言之:有没有办法在使用动态路由时定义类似IndexRoute
的东西?
你应该使用 getIndexRoute
- https://github.com/reactjs/react-router/blob/master/docs/API.md#getindexroutepartialnextstate-callback
由于 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')
} }
})
我有一个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'));
};
以及嵌套动态路由的一些设置:
./routes/Settings/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}
react router passes。例如,如果我导航到 /settings/general
,我将呈现 settings
,这又会呈现 general
作为其子项:
./routes/Settings/General.js
module.exports = {
path: 'general',
getComponent(nextState, cb) {
require.ensure([], (require) => {
cb(null, require('../../screens/AppSettings/General'))
})
}
};
这没问题,但我想做的是定义如果导航到 /settings
.
Settings
简而言之:有没有办法在使用动态路由时定义类似IndexRoute
的东西?
你应该使用 getIndexRoute
- https://github.com/reactjs/react-router/blob/master/docs/API.md#getindexroutepartialnextstate-callback
由于 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')
} }
})