How do I get the RouteParams from a parent component?
App.ts
:
@Component({
...
})
@RouteConfig([
{path: '/', component: HomeComponent, as: 'Home'},
{path: '/:username/...', component: ParentComponent, as: 'Parent'}
])
export class HomeComponent {
...
}
And then, in the ParentComponent
, I can easily get my username param and set the child routes.
Parent.ts
:
@Component({
...
})
@RouteConfig([
{ path: '/child-1', component: ChildOneComponent, as: 'ChildOne' },
{ path: '/child-2', component: ChildTwoComponent, as: 'ChildTwo' }
])
export class ParentComponent {
public username: string;
constructor(
public params: RouteParams
) {
this.username = params.get('username');
}
...
}
But then, how can I get this same 'username' parameter in those child components? Doing the same trick as above, doesn't do it. Because those params are defined at the ProfileComponent or something??
@Component({
...
})
export class ChildOneComponent {
public username: string;
constructor(
public params: RouteParams
) {
this.username = params.get('username');
// returns null
}
...
}
<child-one-component [username]="username"> ...
– Mark Rajcok<routerlink [username]="username">...
? And is that the way to go then @MarkRajcok? – Aico Klein Ovink<a [router-link]="[ './....', {username: username} ]
will work. Sorry, I have no idea if that will work or not. (I haven't played with routing much yet.) – Mark Rajcok<router-outlet></router-outlet>
, should I put an input on that. Because the child routes wil render there.. – Aico Klein Ovink