我有一个路由组件,我想使用webpack进行异步加载:
<Route path="dashboard" getComponent={(location, cb) => {
require.ensure([], (require) => {
cb(null, require('./Containers/Dashboard'));
});
}}>
如果你有很多其他需要异步加载的路由,那么这就是很多样板代码。因此我想,让我们将其重构为一个帮助方法:
const loadContainerAsync = route => (location, cb) => {
require.ensure([], (require) => {
cb(null, require('../Containers/' + route));
});
};
// much 'nicer syntax'
<Route path="dashboard" getComponent={loadContainerAsync('Dashboard')} />
显然,当我查看firefox-devtools中的网络选项卡时,loadContainerAsync函数的行为无法正常运作。你有没有想过这个loadContainerAsync函数可能存在什么问题?