React-router:getChildRoutes未在嵌套的PlainRoute

时间:2016-05-10 22:46:38

标签: reactjs react-router react-redux react-router-redux

作为参考,我使用React-Redux入门套件(https://github.com/davezuko/react-redux-starter-kit)作为基础项目布局。

我有一条名为TSP的路线,我正在使用getComponent和redux connect()在容器组件上注入一个reducer,我希望它能通过children在道具上。

永远不会调用TSP路由定义中的getChildRoutes

根路由器配置:

import ApplicationLayout from '../layouts/ApplicationLayout';
import Home from './Home';

export const createRoutes = (store) => {
  const routes = {
    path: '/',
    component: ApplicationLayout,
    indexRoute: Home,
    getChildRoutes (location, next) {
      require.ensure([], (require) => {
        next(null, [
          require('./TSP').default(store),
          require('./Home').default,
        ]);
      });
    }
  };

  return routes;
};

export default createRoutes;

嵌套路由器配置(TSP):

import { injectReducer } from '../../store/reducers';
import Overview from './routes/Overview';

export default (store) => ({
  path: '/tsp/:id',
  indexRoute: Overview,
  getComponent (nextState, next) {
    require.ensure([
      './containers/TSPContainer',
      './modules/tsp'
    ], (require) => {
      const TSP = require('./containers/TSPContainer').default;
      const reducer = require('./modules/tsp').default;

      injectReducer(store, { key: 'tsp', reducer });
      next(null, TSP);
    });
  },
  getChildRoutes (location, next) {
    debugger
    require.ensure([], (require) => {
      next(null, [
        // Provide store for async reducers and middleware
        require('./routes/Offers').default(store),
        require('./routes/Reviews').default(store)
      ]);
    });
  }
});

我永远无法访问debugger内的getChildRoutes

感谢任何帮助,如果还有更多文件需要我可以添加它们。

2 个答案:

答案 0 :(得分:0)

您是否同步加载了./TSP./Home路由定义?如果没有,我认为如果没有将这些模块指定为依赖项,require.ensure将无效。

所以:

getChildRoutes (location, next) {
  require.ensure([
    './TSP',
    './Home'
  ], (require) => {
    next(null, [
      require('./TSP').default(store),
      require('./Home').default,
    ]);
  });
}

答案 1 :(得分:0)

想出来。上面IndexRoute嵌套路由器配置中的TSP是一个函数,而不是一个对象(我假设react-router在发送getChildRoutes之前期望indexRoute成为一个对象。)< / p>

解决方案是将已解决的Overview的routeConfig分配给indexRoute

<强> TSP/index.js

import { injectReducer } from '../../store/reducers';

export default (store) => ({
  path: '/tsp/:id',
  indexRoute: require('./routes/Overview').default(store),
  getComponent (nextState, next) {
    require.ensure([
      './containers/TSPContainer',
      './modules/tsp'
    ], (require) => {
      const TSP = require('./containers/TSPContainer').default;
      const reducer = require('./modules/tsp').default;

      injectReducer(store, { key: 'tsp', reducer });
      next(null, TSP);
    });
  },
  getChildRoutes (location, next) {
    require.ensure([], (require) => {
      next(null, [
        // Provide store for async reducers and middleware
        require('./routes/Offers').default(store),
        require('./routes/Reviews').default(store)
      ]);
    });
  }
});

<强> TSP/routes/Overview/index.js

export default (store) => ({
  getComponent (nextState, next) {
    require.ensure([
      './components/OverviewView'
    ], (require) => {
      const Overview = require('./components/OverviewView').default;
      next(null, Overview);
    });
  }
});