在 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 中使用它会更好吗?
更好是指性能和逻辑。