app.component.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { Component, ElementRef, OnInit, Renderer2 } from '@angular/core';
  2. import { NavigationEnd, NavigationError, RouteConfigLoadStart, Router } from '@angular/router';
  3. import { TitleService, VERSION as VERSION_ALAIN } from '@delon/theme';
  4. import { environment } from '@env/environment';
  5. import { NzModalService } from 'ng-zorro-antd/modal';
  6. import { VERSION as VERSION_ZORRO } from 'ng-zorro-antd/version';
  7. @Component({
  8. selector: 'app-root',
  9. template: ` <router-outlet></router-outlet> `
  10. })
  11. export class AppComponent implements OnInit {
  12. constructor(
  13. el: ElementRef,
  14. renderer: Renderer2,
  15. private router: Router,
  16. private titleSrv: TitleService,
  17. private modalSrv: NzModalService
  18. ) {
  19. renderer.setAttribute(el.nativeElement, 'ng-alain-version', VERSION_ALAIN.full);
  20. renderer.setAttribute(el.nativeElement, 'ng-zorro-version', VERSION_ZORRO.full);
  21. }
  22. ngOnInit(): void {
  23. let configLoad = false;
  24. this.router.events.subscribe(ev => {
  25. if (ev instanceof RouteConfigLoadStart) {
  26. configLoad = true;
  27. }
  28. if (configLoad && ev instanceof NavigationError) {
  29. this.modalSrv.confirm({
  30. nzTitle: `提醒`,
  31. nzContent: environment.production ? `应用可能已发布新版本,请点击刷新才能生效。` : `无法加载路由:${ev.url}`,
  32. nzCancelDisabled: false,
  33. nzOkText: '刷新',
  34. nzCancelText: '忽略',
  35. nzOnOk: () => location.reload()
  36. });
  37. }
  38. if (ev instanceof NavigationEnd) {
  39. this.titleSrv.setTitle();
  40. this.modalSrv.closeAll();
  41. }
  42. });
  43. }
  44. }