I have the following Angular Route configuration
const appRoutes: Routes = [
{
path: '',
component: DirectoryComponent
},
{
path: 'manage/:type/:id',
component: ManageComponent,
outlet: 'manage',
children: [
{
path: '',
component: PreviewComponent,
pathMatch: 'full'
},
{
path: 'add/:elementType',
component: EditComponent,
}
]
}
];
ManageComponent
is a sandbox component in which PreviewComponent
and EditComponent
will be rendered.
An user use case redirect the user to http://localhost:4200/#/(manage:manage/bar/12762)
which match the preview component. Everything is okay here.
From the PreviewComponent
and when the user clicks on a button, I want to make a relative navigation to the EditComponent
to have, when the navigation finish, http://localhost:4200/#/(manage:manage/bar/12762/add/foo)
I tried
this.router.navigate([{outlets: {manage: ['add', 'foo']}}],{relativeTo: this.route});
and
this.router.navigate([{outlets: {manage: ['add', 'foo']}}]);
But every time, user is redirected to http://localhost:4200/#/add/foo
.
The Router-Outlet is a directive that's available from the router library where the Router inserts the component that gets matched based on the current browser's URL. You can add multiple outlets in your Angular application which enables you to implement advanced routing scenarios.
What is the difference between [routerLink] and routerLink ? How should you use each one? They're the same directive. You use the first one to pass a dynamic value, and the second one to pass a static path as a string.
So by the use of relative path you are making your links free that are relative to the current URL segment. Your feature area of routing will be the same even if you change the route for the parent. You are just making your link free even if you change the parent route path.
I know that it's an old question, but I found a solution while I'm looking for an answer.
You can use { relativeTo: this.activatedRoute.parent }
and everything works like a charm.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With