1

in my route config I have a route with children set, something like this:

 {
    path: 'users',
    children: [
        { path: ':id', component: UserDetailsComponent },
        { path: '', component: UserListComponent }
    ]
 }

now, in my UserListComponent I need to build a link to user profile on the fly, having the path and the id value.... so how would I do that, providing that I have injected successfully the router (private router: Router)

so I would like to do is

const link = this.router.createUrl('/users', {id:item.id});

and I would get something like this

/users/xyse1334

DS_web_developer
  • 3,622
  • 13
  • 52
  • 83
  • The current router doesn't have a `createUrl()` method. What router version are you using? What do you want to use this URL for anyway or how do you want to use it? – Günter Zöchbauer Jul 07 '16 at 16:39
  • I know it doesn't have it... I said what I would like ;) I am using the latest version, I want to use the same syntax as router.navigate method... so: this.router.navigate(['/users', users.id]) probably.... but instead of actually navigating away wold just love to get the generated URL so I can stick it into the href ..... would do it in the view, but this is part of a complicated/dynamic content creator... that has plenty of business logic which needs to stay in the component – DS_web_developer Jul 07 '16 at 16:43
  • I don't get it. Why would you want to stick it into href? That's what the router is doing for you already. You can just do `'/users/${item.id}'` – Günter Zöchbauer Jul 07 '16 at 16:45
  • so, I am building a table from a json data.... and a column value could have different modifiers (depending on the content type actually)... so if it's a number it would just display a number, if it's a link it would have to create an A tag and add the href attr depending on the settings... it's very dynamic..and I cannot really put this logic into the view for every single column – DS_web_developer Jul 07 '16 at 16:53
  • You can use `*ngIf` to decide if a number should be added or a `` – Günter Zöchbauer Jul 07 '16 at 16:57

1 Answers1

0

For route parameters just pass them like path elements

const link = '/users/${item.id}';
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • well, this is pretty static... I need to be able to pass queryParams dynamically and router needs to map it to the configured path automatically.... the router.navigate works this way – DS_web_developer Jul 07 '16 at 16:50
  • I don't get what the problem is. If you need query parameters, why don't you just add `?xxx=yyy` (or `;xxx=yyy` for matrix parameters). What is too static about creating a string from dynamic parts? – Günter Zöchbauer Jul 07 '16 at 16:54