2016-09-29 27 views
2

我使用与反应路由器的WebPack代码分裂和异步I做到这一点加载部件:检查出Henley Edition's article抽象require.ensure样板从反应路由器getComponent不起作用

<Route 
    path="somePath" 
    getComponent={(next, cb) => { 
    require.ensure([], (require) => { 
     cb(null, require('../components/Example.jsx')); 
    }); 
    } 
} 
/> 

现在我尝试做这样的事情,以避免使用捆绑加载器,并仍然减少样板。

const loadLazy = (component) => { 
    return (next, cb) => { 
    require.ensure([], (require) => { 
     cb(null, require(component)); 
    }); 
    }; 
}; 

... 

<Route 
    path="somePath" 
    getComponent={lazyLoad('../components/Example.jsx')} 
/> 

但是这在控制台抛出错误说找不到模块'../components/Example.jsx'。

为什么这不起作用?

回答

1

require.ensure不适用于动态模块路径(如您的require(component)),这就是为什么您引用的文章使用Webpack's bundle-loader来代替此限制。