3

是否有可能实现嵌套异步路由获取,System.import因此应用程序仅分为几个合理的块,而不是许多小块?

背景

我正在我的 React 应用程序中实现捆绑拆分。最初我实现了 bundle-loader(我无法在所有情况下都开始工作),然后使用 System.import,我发现它的行为更加可预测。

问题

代码拆分在逐个路由的基础上工作得很好,但是,它会产生许多小包,并且额外的包和获取的开销是不必要的和浪费的。

例如,我有这段代码,当您导航到他们的页面时,它会为仪表板、设置或图像加载包:

    <Provider store={store}>
        <Router history={browserHistory}>
            <Route path='/' component={Container}>
            <IndexRedirect to="Login" />
            <Route path='Registration' component={RegistrationContainer} />
            <Route path='Login' component={LoginContainer} />
            <Route path='Route1' >
                <IndexRedirect to="Dashboard" />
                <Route path='Settings' 
                        getComponent={ (loc, cb)=> {System.import('./components/Route1/Settings')
                                              .then(loadRoute(cb))
                                              .catch(errorLoading); }}/>
                <Route path='Dashboard' 
                        getComponent={ (loc, cb)=> {System.import('./components/Route1/Dashboard')
                                              .then(loadRoute(cb))
                                              .catch(errorLoading); }}
                    />
                <Route path='Images' 
                        getComponent={ (loc, cb)=> {System.import('./components/Route1/Images')
                                              .then(loadRoute(cb))
                                              .catch(errorLoading); }}
                    />
        </Router>
    </Provider>

首次导航到 Route1 路径时,是否可以为所有三个加载捆绑包?

非常感谢

4

1 回答 1

2

我已经尝试并设法找到一种解决方案,可以将任何给定数量的子路由的 get 请求数量减少到 2。

我得到这个结果的方法是在 Route1 中引入一个索引组件

<Route path='Patient' getComponent={ (loc, cb)=> {System.import('./components/Route1Index')
            .then(loadRoute(cb))
            .catch(errorLoading); }}>

然后我为所有子路由创建了索引组件,如下所示:

import React from 'react';

// Child route components
  import Settings from './Settings';
  import Dashboard from './Dashboard';
  import Images from './Images';

export default (props) =>(
  <div>
        {props.children}
  </div>
)

我也在minChunks:2Webpack中设置CommonsChunkPlugin

现在,当我导航到 Route1 的任何子路由时,只加载了 2 个包:Route1Index包和包含所有导入的包

于 2017-02-17T16:48:28.857 回答