【发布时间】:2017-07-01 22:38:04
【问题描述】:
在 react-router v3 中,我使用 System.import 实现了代码拆分,现在我想将我的应用升级到 react-router-v4,但问题是我无法拆分我的代码。
我的
routes.js文件
function errorLoading(error) {
throw new Error(`Dynamic page loading failed: ${error}`);
}
function loadRoute(cb) {
return module => cb(null, module.default);
}
module.exports = {
path: '/',
indexRoute: {
getComponent(location, cb) {
System.import('../pages/IndexPage')
.then(loadRoute(cb))
.catch(errorLoading);
}
},
childRoutes: [{
path: 'notifications',
indexRoute: {
getComponent(location, cb) {
System.import('../pages/NotificationPage')
.then(loadRoute(cb))
.catch(errorLoading);
}
},
}]
};
然后我只是在我的index.js 文件中导入路由并将它们渲染到 rootNode 像
ReactDOM.render(
<AppContainer>
<ApolloProvider store={store} client={client}>
<Router
history={browserHistory}
routes={routes}
/>
</ApolloProvider>
</AppContainer>,
rootNode
);
【问题讨论】:
-
这是一个很棒的视频教程,其中包含代码示例,您可以按照这些示例设置使用 React Router 4+ 的代码拆分youtu.be/j8NJc60H294
标签: webpack-2 react-router-v4 react-router-dom