带有Lazy-Loaded路由的Angular CLI HMR会重新加载整个内容

Aug*_*ner 62 webpack-hmr angular-cli hot-module-replacement angular angular7

(即使使用Angular 7也确认了问题).让我们解决这个问题!

我正在使用HMR设置:https://github.com/angular/angular-cli/wiki/stories-configure-hmr from a ng newbuild build.

如果我更改任何组件并使其延迟加载,角度HMR将热重新加载所有内容,使页面同步更慢.

我知道这是因为它默认设置为console.log每个正在重新加载的模块,当我使用惰性路由时,它会记录所有内容.但是当我将该组件更改为非延迟加载时,它只记录一些小组件.

因此,当我使用HMR和懒惰路由时,我的应用程序需要几秒钟才能刷新.这很烦人.

有没有办法解决?

(懒惰的加载路线是通过这样的方式完成的)

// Main homepage
{
  path: '',
  loadChildren: './public/home/home.module#HomeModule'
},
// ...
Run Code Online (Sandbox Code Playgroud)

(只是一个例子,表明我正在以正确的方式加载)

这里有一些日志记录显示我懒惰加载时会发生什么 home.component.ts

// Everything here is normal, great!
[HMR]  - ../../../../../src/app/public/home/home.component.html
log.js:23 [HMR]  - ../../../../../src/app/public/home/home.component.ts
log.js:23 [HMR]  - ../../../../../src/app/public/home/home.module.ts
log.js:23 [HMR]  - ../../../../../src/app/public/home/home.routing.ts
// Everything below here is NOT normal, bad!  All this is extra.  These are my modules, yes, but all this needs to be loaded again?
log.js:23 [HMR]  - ../../../../../src/$$_lazy_route_resource lazy recursive
log.js:23 [HMR]  - ../../../core/esm5/core.js
log.js:23 [HMR]  - ../../../platform-browser-dynamic/esm5/platform-browser-dynamic.js
log.js:23 [HMR]  - ../../../common/esm5/common.js
log.js:23 [HMR]  - ../../../platform-browser/esm5/platform-browser.js
log.js:23 [HMR]  - ../../../router/esm5/router.js
log.js:23 [HMR]  - ../../../../../src/app/shared/shared.module.ts
log.js:23 [HMR]  - ../../../common/esm5/http.js
log.js:23 [HMR]  - ../../../../../src/app/features/proxy-http-interceptor/proxy-http-interceptor.ts
log.js:23 [HMR]  - ../../../../../src/app/shared/unauthorized-http-interceptor.ts
log.js:23 [HMR]  - ../../../../../src/app/features/auth/auth.service.ts
log.js:23 [HMR]  - ../../../../../src/app/features/user/user.service.ts
log.js:23 [HMR]  - ../../../../@auth0/angular-jwt/index.js
log.js:23 [HMR]  - ../../../../@auth0/angular-jwt/src/jwt.interceptor.js
log.js:23 [HMR]  - ../../../../@auth0/angular-jwt/src/jwthelper.service.js
log.js:23 [HMR]  - ../../../../@auth0/angular-jwt/src/jwtoptions.token.js
log.js:23 [HMR]  - ../../../../../src/app/shared/container.directive.ts
log.js:23 [HMR]  - ../../../flex-layout/esm5/flex-layout.es5.js
...
...
A ton more logging
Run Code Online (Sandbox Code Playgroud)

Nic*_*oub 5

angular-cli 维基页面已弃用:https ://github.com/angular/angular-cli/issues/14053

目前不支持 Angular-HMR + 惰性路由:https ://github.com/PatrickJS/angular-hmr/issues/76

对于有效且重要的解决方案,请查看https://github.com/wags1999/angular-hmr-lazy-components

  • 我之前也注意到了这里提出的“解决方案”:https://github.com/wags1999/angular-hmr-lazy-components,这是一个有点麻烦的过程。虽然我尝试在当前的项目中实现它但没有成功,但根据评论,我相信它可以在不太复杂的项目上使用。然而,由于“解决方案”的总体复杂性以及其实施的整体混乱,我不认为这可以被授予这个 SO 问题的“解决方案”。 (3认同)