How to generate url path in new Router Angular 2

m.artamoshkin picture m.artamoshkin · Oct 12, 2016 · Viewed 15.8k times · Source

What analog deprecated-router method generate in new Router 3.0.0? Early it can take something like this:

this._router.generate(['Profile']).urlPath;

How do it on new router?

Answer

Günter Zöchbauer picture Günter Zöchbauer · Oct 12, 2016

https://angular.io/docs/ts/latest/api/router/index/Router-class.html#!#createUrlTree-anchor

var urlTree = this._router.createUrlTree(['Profile']);

You can pass the result to

this._router.navigate(/*string|UrlTree);

or get the URL

var url = this._router.createUrlTree(['Profile']).toString();

Edit: As of Angular 6, you pass the result to NavigateByUrl:

this._router.navigateByUrl(string|UrlTree);