Search code examples
angularangular-routing

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


Solution

  • 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"