首页 > 解决方案 > Angular5 - 如何检查模块是否已加载

问题描述

在 Angular 5 中,我需要知道模块是否已经加载,以免显示微调器。目前我的代码是这样的:

    constructor(private loaderService: LoaderService, private router: Router, @Inject(DOCUMENT) private document: Document) {
    this.router.events.subscribe(event => {
        console.log(this.router);
        if (event instanceof NavigationStart) {
            this.isSpinnerVisible = true;
        } else if (event instanceof NavigationEnd || event instanceof NavigationCancel || event instanceof NavigationError) {
            this.isSpinnerVisible = false;
        }
    }, () => {
        this.isSpinnerVisible = false;
    });
}

这样,如果模块已经被加载,我仍然显示模块十分之一秒。有什么想法?

标签: angularmodulenavigationangular5

解决方案


您可以使用全局变量(是的,不建议这样做,但它们的存在是有原因的,所以利用它)

export class MyAppModule {
  constructor() {
    if (!window['loaded_modules']) { window['loaded_modules'] = []; }
    window['loaded_modules'].push(this.constructor.name);
  }
}

推荐阅读