Get page name/component name in Angular Typescript

Himalaya Pagada
Jun 16, 2022

Get the page name from the URL in the Angular typescript page. This name will equal the mentioned name on the routing module page. You can use this for page-specific conditions.

Syntax

import { NavigationEnd, Router } from ‘@angular/router’;
import { filter } from ‘rxjs/operators’;

pageName: any = ‘’;constructor(public router: Router) {router.events.pipe(filter(event => event instanceof NavigationEnd)).subscribe((res: any) => {if (res.url != ‘/’) {this.pageName = res.url.split(‘/’).pop();this.pageName = this.pageName.split(‘?’).shift();}else {this.pageName = ‘/’;}});}

--

--