Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Route Params in Child Routes

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'}
  ]
like image 445
Nik Avatar asked Nov 25 '16 02:11

Nik


People also ask

What is the difference between a basic route and a child route?

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.

Can we have two router outlet?

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.

How do you add a child to routing?

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).

Which property is used to define child routes within a routes object?

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.


1 Answers

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
like image 64
rod750 Avatar answered Sep 28 '22 21:09

rod750