我最近将我的角度应用更新到了最新版本。经过一夜的噩梦之后,除了HMR之外,我得到了一切。我坚持不懈。以下是根据Angular CLI wiki上的HMR故事的配置:
angular.json
"build": {
"configurations": {
"hmr": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.hmr.ts"
}
]
}
}
},
"serve": {
"configurations": {
"hmr": {
"hmr": true,
"browserTarget": "appHit:build:hmr"
},
}
},
hmr.js
import { NgModuleRef, ApplicationRef } from '@angular/core';
import { createNewHosts } from '@angularclass/hmr';
export const hmrBootstrap = (module: any, bootstrap: () => Promise<NgModuleRef<any>>) => {
let ngModule: NgModuleRef<any>;
module.hot.accept();
bootstrap().then(mod => ngModule = mod);
module.hot.dispose(() => {
const appRef: ApplicationRef = ngModule.injector.get(ApplicationRef);
const elements = appRef.components.map(c => c.location.nativeElement);
const makeVisible = createNewHosts(elements);
ngModule.destroy();
makeVisible();
});
};
main.ts
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
import { hmrBootstrap } from './hmr';
if (environment.production) {
enableProdMode();
}
const bootstrap = () => platformBrowserDynamic().bootstrapModule(AppModule);
if (environment.hmr) {
if (module[ 'hot' ]) {
hmrBootstrap(module, bootstrap);
} else {
console.error('HMR is not enabled for webpack-dev-server!');
console.log('Are you using the --hmr flag for ng serve?');
}
} else {
bootstrap().catch(err => console.log(err));
}
我尝试了以下命令:
ng serve --hmr
ng serve --hmr --configuration hmr
ng serve --configuration hmr
所有内容都会在更改时编译,甚至触发的事件都会缓存在浏览器中,但HMR记录以下内容后没有任何反应:
[WDS] App updated. Recompiling...
[WDS] App hot update...
此时我完全迷失了。任何形式的帮助将非常感谢。谢谢
答案 0 :(得分:2)
我们遇到了同样的问题。通过删除与Webpack相关的开发人员依赖项并执行全新的npm安装,可以解决该问题。
rm -R package-lock.json node_modules
npm cache clean --force
npm i
希望它可以提供帮助。
答案 1 :(得分:0)
有点晚了...对我有用的是在我的ClientApp文件夹中,我有一个/ dist文件夹,带有所有js位...如果有dist文件夹,HMR将无法工作。如果您进行构建,则dist到达那里。删除dist文件夹,然后运行ng serve。以我为例,我托管了一个.net核心应用程序,因此我构建了该应用程序并针对我的角度应用程序进行了启动。
答案 2 :(得分:0)
我认为这可能对某些人有帮助。我通过更新到Angular版本7并在main.ts中为HMR添加以下行来解决了我的问题
module['hot'].accept();
如下:
if (environment.hmr) {
if (module['hot']) {
module['hot'].accept();
hmrBootstrap(module, bootstrap);
} else {
console.error('HMR is not enabled for webpack-dev-server!');
console.log('Are you using the --hmr flag for ng serve?');
}
} else {
console.log('hot');
bootstrap().catch(err => console.log(err));
}
到目前为止,HMR一直运行良好。我没有时间进一步调试它,但很可能是依赖性不兼容可能导致了它的终止
答案 3 :(得分:-1)
这是我的设置,目前在最新版本下工作得很好。如果您不需要,可以删除ngrx
内容: - )
// main.ts
// ...
import {
bootloader, createInputTransfer, createNewHosts, removeNgStyles
} from '@angularclass/hmr/dist/helpers'; // For correct treeshaking
if (environment.production) {
enableProdMode();
}
type HmrModule<S> = {
appRef: ApplicationRef,
}
type HmrNgrxModule<S, A> = HmrModule<S> & {
store: { dispatch: (A) => any } & Observable<S>,
actionCreator: (s: S) => A
}
const isNgrxModule =
<S, A, M extends HmrNgrxModule<S, A>>(instance: HmrModule<S> | HmrNgrxModule<S, A>)
: instance is M =>
!!((<M>instance).store && (<M>instance).actionCreator);
function processModule<S, A, M extends HmrModule<S> | HmrNgrxModule<S, A>>(ngModuleRef: NgModuleRef<M>) {
const hot = module['hot'];
if (hot) {
hot['accept']();
const instance = ngModuleRef.instance;
const hmrStore = hot['data'];
if (hmrStore) {
hmrStore.rootState
&& isNgrxModule(instance)
&& instance.store.dispatch(instance.actionCreator(hmrStore.rootState));
hmrStore.restoreInputValues && hmrStore.restoreInputValues();
instance.appRef.tick();
Object.keys(hmrStore).forEach(prop => delete hmrStore[prop]);
}
hot['dispose'](hmrStore => {
isNgrxModule(instance) && instance.store.pipe(take(1)).subscribe(s => hmrStore.rootState = s);
const cmpLocation = instance.appRef.components.map(cmp => cmp.location.nativeElement);
const disposeOldHosts = createNewHosts(cmpLocation);
hmrStore.restoreInputValues = createInputTransfer();
removeNgStyles();
ngModuleRef.destroy();
disposeOldHosts();
});
}
else {
console.error('HMR is not enabled for webpack-dev-server!');
console.log('Are you using the --hmr flag for ng serve?');
}
return ngModuleRef;
}
const bootstrap = () => platformBrowserDynamic().bootstrapModule(AppModule);
const hmrBootstrap = () => bootloader(() => bootstrap().then(processModule));
environment.hmr
? hmrBootstrap()
: bootstrap();
// app.module.ts
// ...
export class AppModule {
constructor(
public appRef: ApplicationRef
// , ...
){}
}
// angular.json
"build": {
"configurations": {
"hmr": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.hmr.ts"
}
]
}
}
},
"serve": {
"configurations": {
"hmr": {
"browserTarget": "AppName:build:hmr"
}
}
}
我用ng serve --hmr -c=hmr
我希望这有点帮助: - )