app.component.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import { Component, ElementRef, OnInit, Renderer2 } from '@angular/core';
  17. import { NavigationEnd, NavigationError, RouteConfigLoadStart, Router } from '@angular/router';
  18. import { TitleService, VERSION as VERSION_ALAIN } from '@delon/theme';
  19. import { environment } from '@env/environment';
  20. import { NzModalService } from 'ng-zorro-antd/modal';
  21. import { VERSION as VERSION_ZORRO } from 'ng-zorro-antd/version';
  22. @Component({
  23. selector: 'app-root',
  24. template: ` <router-outlet></router-outlet> `
  25. })
  26. export class AppComponent implements OnInit {
  27. constructor(
  28. el: ElementRef,
  29. renderer: Renderer2,
  30. private router: Router,
  31. private titleSrv: TitleService,
  32. private modalSrv: NzModalService
  33. ) {
  34. renderer.setAttribute(el.nativeElement, 'ng-alain-version', VERSION_ALAIN.full);
  35. renderer.setAttribute(el.nativeElement, 'ng-zorro-version', VERSION_ZORRO.full);
  36. }
  37. ngOnInit(): void {
  38. let configLoad = false;
  39. this.router.events.subscribe(ev => {
  40. if (ev instanceof RouteConfigLoadStart) {
  41. configLoad = true;
  42. }
  43. if (configLoad && ev instanceof NavigationError) {
  44. this.modalSrv.confirm({
  45. nzTitle: `提醒`,
  46. nzContent: environment.production ? `应用可能已发布新版本,请点击刷新才能生效。` : `无法加载路由:${ev.url}`,
  47. nzCancelDisabled: false,
  48. nzOkText: '刷新',
  49. nzCancelText: '忽略',
  50. nzOnOk: () => location.reload()
  51. });
  52. }
  53. if (ev instanceof NavigationEnd) {
  54. this.titleSrv.setTitle();
  55. this.modalSrv.closeAll();
  56. }
  57. });
  58. }
  59. }