Angular · Senior · Question 64 of 75
How do you show a global progress bar while a lazy route is loading?
inject(Router).events.subscribe((e) => {
if (e instanceof NavigationStart) this.loading.set(true);
if (e instanceof NavigationEnd || e instanceof NavigationCancel || e instanceof NavigationError)
this.loading.set(false);
});- ✓Correct answer: Listen to router events and stop on
NavigationEnd,NavigationCancelandNavigationError - 2Stop only on
NavigationEnd - 3Use
@deferaround<router-outlet>
Explanation
If cancel and error events are ignored, a blocked guard or a failed chunk leaves the spinner on forever.