在反應路由器v3我已經實現了代碼分裂使用System.import
,現在我想升級我的應用程序react-router-v4,但問題是,我無法分割我的代碼。使用反應路由器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
文件,並將它們呈現給根節點像
ReactDOM.render(
<AppContainer>
<ApolloProvider store={store} client={client}>
<Router
history={browserHistory}
routes={routes}
/>
</ApolloProvider>
</AppContainer>,
rootNode
);
感謝codeBelt它的工作,我還發現了一個更好的溶膠而不使用任何外部軟件包 – sbs