4

In an Angular2 guard you get access to an ActivatedRouteSnapshot object. Is there an easy way to recreate the array that can be used as a routeLink?

So when the guard is fired for the route /section/:id/action the ActivatedRouteSnapshot contains a bunch of information, but to create a link to that specific route you need an array ['/section', '<:id value>', 'action'].

Ofcourse I can just split and whatnow, but considering the complexity that is possible when taking parameters into account I prefer to use the 'standard' way if there is one.

Thanks in advance.

Robba
  • 7,684
  • 12
  • 48
  • 76

1 Answers1

2

I think you're looking for the url function on the ActivatedRoute snapshot. Use it like this.

constructor(private route: ActivatedRoute){}

ngOnInit(){
    const urlArr = this.route.snapshot.url;
    // urlArr = ["blah", "blah", "blah"]
}

There's a bunch of other ways you could do it, but ActivatedRoute snapshot is how I would. How to get current route

Community
  • 1
  • 1
Federico Pettinella
  • 1,471
  • 1
  • 11
  • 19