Error when trying to lazy load feature modules using angular-cli with webpack

后端 未结 4 633
旧时难觅i
旧时难觅i 2020-12-31 18:49

Hi I\'m trying to use angular-cli with webpack (+productivity) to build my angular2 app but I\'m having issues when trying to lazy load modules which were working with versi

4条回答
  •  鱼传尺愫
    2020-12-31 19:46

    There's a loader for this (angular2-router-loader). However you can't use it with the CLI without hacking the config. Luckily, es6-promise-loader does work with the CLI out-of-the-box.

    This is what worked out for me:

    First we'll need the es6-promise-loader:

    npm i --save-dev es6-promise-loader

    Then define your route like this:

    {path:"lazy", loadChildren: () => require('es6-promise!./path/to/module')('ClassName')}

    the es6-promise-loader replaces the above with this:

    loadChildren: () => new Promise(function (resolve) {
            require.ensure([], function (require) {
              resolve(require('./path/to/module')['ClassName']));
            });
          });
    

    which is the proper way to load a module with webpack, but cumbersome to put into every route.

提交回复
热议问题