hmr.ts 776 B

123456789101112131415161718192021
  1. import { NgModuleRef, ApplicationRef } from '@angular/core';
  2. import { createNewHosts } from '@angularclass/hmr';
  3. import { NzModalService } from 'ng-zorro-antd';
  4. export const hmrBootstrap = (
  5. module: any,
  6. bootstrap: () => Promise<NgModuleRef<any>>,
  7. ) => {
  8. let ngModule: NgModuleRef<any>;
  9. module.hot.accept();
  10. bootstrap().then(mod => (ngModule = mod));
  11. module.hot.dispose(() => {
  12. const appRef: ApplicationRef = ngModule.injector.get(ApplicationRef);
  13. const modalService = ngModule.injector.get(NzModalService, null) as NzModalService;
  14. if (modalService) modalService.closeAll();
  15. const elements = appRef.components.map(c => c.location.nativeElement);
  16. const makeVisible = createNewHosts(elements);
  17. ngModule.destroy();
  18. makeVisible();
  19. });
  20. };