23

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?

2
  • What do you need to do? Commented Oct 12, 2016 at 15:23
  • @AlexanderCiesielski I need to get concrete route path. Commented Oct 12, 2016 at 15:30

2 Answers 2

36

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);
Sign up to request clarification or add additional context in comments.

1 Comment

I thought, it create new route. Thank you!
10

These are the current examples from the page referenced by Günter

// create /team/33/user/11
router.createUrlTree(['/team', 33, 'user', 11]);

// create /team/33;expand=true/user/11
router.createUrlTree(['/team', 33, {expand: true}, 'user', 11]);

// you can collapse static segments like this (this works only with the first passed-in value):
router.createUrlTree(['/team/33/user', userId]);

// If the first segment can contain slashes, and you do not want the router to split it, you
// can do the following:

router.createUrlTree([{segmentPath: '/one/two'}]);

// create /team/33/(user/11//right:chat)
router.createUrlTree(['/team', 33, {outlets: {primary: 'user/11', right: 'chat'}}]);

// remove the right secondary node
router.createUrlTree(['/team', 33, {outlets: {primary: 'user/11', right: null}}]);

// assuming the current url is `/team/33/user/11` and the route points to `user/11`

// navigate to /team/33/user/11/details
router.createUrlTree(['details'], {relativeTo: route});

// navigate to /team/33/user/22
router.createUrlTree(['../22'], {relativeTo: route});

// navigate to /team/44/user/22
router.createUrlTree(['../../team/44/user/22'], {relativeTo: route});

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.