How do you detect route change in Angular?
In Angular7, you can subscribe to router to detect the changes. The subscription for router events would be as below,
this.router.events.subscribe((event: Event) => {});
Let's take a simple component to detect router changes
import { Component } from '@angular/core';import { Router, Event, NavigationStart, NavigationEnd, NavigationError } from '@angular/router';@Component({selector: 'app-root',template: `<router-outlet></router-outlet>`})export class AppComponent {constructor(private router: Router) {this.router.events.subscribe((event: Event) => {if (event instanceof NavigationStart) {// Show loading indicator and perform an action}if (event instanceof NavigationEnd) {// Hide loading indicator and perform an action}if (event instanceof NavigationError) {// Hide loading indicator and perform an actionconsole.log(event.error); // It logs an error for debugging}});}}
September 21, 2022
535
Read more
What is Angular Framework?
November 04, 2022
AngularWhat is a Angular module?
November 03, 2022
AngularWhat are the steps to use animation module?
October 31, 2022
Angular