0

我有一个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}反应路由器传递。例如,如果我导航到/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在使用动态路由时定义类似的东西?

4

2 回答 2

1

你应该使用getIndexRoute- https://github.com/reactjs/react-router/blob/master/docs/API.md#getindexroutepartialnextstate-callback

于 2016-09-11T18:20:57.147 回答
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')
  } }

})
于 2017-07-12T18:20:40.720 回答