Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get activated route from angular 2 routing

Hi so I have declared my route with the id like this {path: 'spreadsheet/:id', component: ContactParent} so here I can get the id by using ActivatedRoute but how I can get spreadsheet, if I do this.router.url it gives me spreadsheet/20 but I only need spreadsheet

like image 691
Dheeraj Agrawal Avatar asked Nov 18 '22 08:11

Dheeraj Agrawal


2 Answers

Try this...

constructor( private route: ActivatedRoute ) {
    console.log(route.pathFromRoot[1].snapshot.url[0].path);
}

If you go to, for example, http://localhost:4200/spreadsheet/3, the above code will log "spreadsheet"

like image 52
Joshua Craven Avatar answered Dec 26 '22 19:12

Joshua Craven


import {Router} from '@angular/router'; // import Router from @angular/router

constructor(private router: Router){ // private member of Router
 const currentPageUrl = router.url; // router.url contain the active route info
 console.log('Activated router is ' + currentPageUrl);
}
like image 34
Shubham Jain Avatar answered Dec 26 '22 19:12

Shubham Jain