I have a route: /app/project/23/views
Each part of the path is a child of the previous part and all children are lazy loaded (not sure if the lazy loaded bit here is relevant)
/app (root component)
/project (child component of app, child route of app)
/23 (route param 'projectId')
/views (child component of project, child route of project)
I'm trying to access the route parameter 'projectId' (in this case: 23)
Doing the following:
constructor(private route: ActivatedRoute) {}
ngOnInit() {
this.route.params.subscribe(params => {
console.log('params:', params);
}
Gives me an empty object for this.route.params
params: {}
However doing this:
constructor(private route: ActivatedRoute) {}
ngOnInit() {
this.route.parent.parent.params.subscribe(params => {
console.log('params:', params);
}
Gives me what I'm looking for:
params: Object {projectId: "23"}
Because it is a child route, do I have to specify this.route.parent.parent to get the route params of (some) parent route?
Is there any better way of getting the params from the full route because what happens if:
1) I change the depth of the child route and move it one more step down. Do I have to change my code now to say this.route.parent.parent.parent.params
2) Or say it's the 10th (generation?, sub-child?, great-great-great-grandchild?). Do I then have to say: this.route.parent.parent.parent.parent.parent.parent.parent.parent.parent.params
--- Edit: added routing config files ---
app.routing.ts
const routes: Routes = [
{path: 'app', loadChildren: './views/private/private.module#PrivateModule'}
];
private.routing.ts
const routes: Routes = [{
path: '',
component: PrivateComponent,
children: [
{path: 'project', loadChildren: './project/project.module#ProjectModule'},
{path: '', redirectTo: 'project', pathMatch: 'full'}
]
project.routing.ts
const routes: Routes = [{
path: ':projectId',
component: ProjectComponent,
children: [
{path: 'views', loadChildren: './views/views.module#ViewsModule'},
{path: '', redirectTo: 'views', pathMatch: 'full'}
]
A child route is like any other route, in that it needs both a path and a component . The one difference is that you place child routes in a children array within the parent route.
You can have multiple router-outlet in same template by configuring your router and providing name to your router-outlet, you can achieve this as follows. Advantage of below approach is thats you can avoid dirty looking URL with it. eg: /home(aux:login) etc.
You can create a nested routing by defining child routes using the children property of a route (alongside a path and component properties). You also need to add a nested router-outlet in the HTML template related to the component linked to the parent route (In our case it's the admin route).
In Angular, the router lets you add child routes using the children property inside the routing module. Here you can see that the routing module has been updated with the child route and added to the array of components so we do not need to import all of them wherever we go.
You can access the tree of the route with pathFromRoot
property, it returns an array of ActivatedRoutes
with all parents.
this.route.pathFromRoot[2].params
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