Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serialize ActivatedRoute to string in Angular 2?

Tags:

angular

I'm trying to build breadcrumb to display in my "shell" component (one with <router-outlet>). I'm injecting Angular's Router service, and by traversing it from routerState.root down through its firstChild (or children[0]) property until null, building the (linked) list of components comprising currently displayed route. The result is a linked list of ActivatedRoute objects. As I'm attaching custom data to each defined route (e.g. title), that I can get (via ActivatedRoute.data member) at runtime, so I can display names of items in breadcrumbs.

But the problem is with links (urls). ActivatedRoute has all the info about particular route in chain, but I couldn't find a way to serialize it to single string value, to use as a link in breadcrumb. I could manually parse it of course, but this is waaaay too much work for something so essential. ActivatedRoute has urls (itself split in segments), query params, matrix params, etc. all in different properties (some of them even Observables, to unnecessarily complicate things)... but no single string property that gives complete urls (with params and everything).

Incidentally, the Router service has serializeUrl() member that takes parameter of type UrlTree, and converts it to string. Maybe there is some method that converts ActivatedRoute into UrlTree, so that I could then use Router.serializeUrl()?..

Simply: How can I get serialized string out of ActivatedRoute (or ActivatedRouteSnapshot) object? (Without writing the whole parsing logic myself obviously).

It seems so essential thing to need, as ActivatedRoute is essentially parsed representation of string url (if I'm understanding this whole concept correctly)... and Router even has helper method to stringify UrlTree object (which is never explained how to retrieve in the first place)...

like image 965
Titan Avatar asked Dec 26 '16 21:12

Titan


People also ask

What does ActivatedRoute do in Angular?

ActivatedRoutelink. Provides access to information about a route associated with a component that is loaded in an outlet.

What is difference between router and ActivatedRoute?

Router does redirecting, don't call APIs about "getting the route information". If you needed to subscribe to changes in route, use this. router . ActivatedRoute does all "get the query params, get current URL, etc".

What is ActivatedRoute in Angular example?

It carries the information about a route linked to a component loaded into the Angular app template. An ActivatedRoute contains the router state tree within the angular app's memory. Make sure to remove strict type warnings or error make sure to set “strict”: false under compilerOptions property in tsconfig. json file.

What is UrlTree in Angular?

UrlTree is a data structure that provides a lot of affordances in dealing with URLs.


1 Answers

router.createUrlTree can receive an ActivatedRoute.

@Component({
  selector: "foo"
})
class Foo {
  constructor(router: Router, route: ActivatedRoute) {
    const urlTree = router.createUrlTree(["."], { relativeTo: route });
    console.log(router.serializeUrl(urlTree));
  }
}
like image 93
ukyo Avatar answered Oct 03 '22 19:10

ukyo