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);
});

Explanation

If cancel and error events are ignored, a blocked guard or a failed chunk leaves the spinner on forever.

Further reading

Problem with a question

Angular · Senior · question 64

Test yourself

Take the Angular · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz