在两个不同的类中监听 router.events ,可以吗?

时间:2021-07-07 11:43:20

标签: angular router

AppComponent 中,我正在监听路由器事件以制作一些东西。

export class AppComponent {
   public ngOnInit(): void {
    this.router.events
      .pipe(
        ...
      ).subscribe(...);
   }
}

我有一个 LoaderModule 并且在他的构造函数中我也监听路由器事件


export class LoaderRouterModule {
  constructor(private readonly router: Router, private readonly loaderService: LoaderService) {
    this.router.events.subscribe((event: Event) => {
      if (event instanceof NavigationStart) {
        this.loaderService.start();
      }

      if (
        event instanceof NavigationError ||
        event instanceof NavigationEnd ||
        event instanceof NavigationCancel
      ) {
        this.loaderService.stop();
      }
    });
  }

这个模块是在AppModule

中导入的

在两个不同的类中监听路由器事件是一种不好的做法?

如果我从 LoaderModule 中删除 router.events 并在 AppComponent 中使用它会更好吗?

更好是指性能和逻辑。

0 个答案:

没有答案
相关问题