Child Routing
redirectTo: attribute in the app-routing module
This attribute is used to redirect to the specific action. Below we are redirecting to the “employee”
action.
Output:
Every time we hit the localhost: 4200/ it will redirect to localhost:4200/employee.
Step 1: create one component which we want to make a child component of already present
component.
Now we have create child component as department-child of department-list component
Step 2: create a child ‘dept-child’ routing in department-list routing
Step 3: want to call the department and dept-child routing at same time from [Link]
Step 4: create object of Router in [Link] in constructor
Step 5: add navigate method from route object, this will navigate to that particular routing.
After clicking on employee button, this will navigate to ‘emloyee’ routing and after clicking on
department button, it will route to department component. PFB screenshot,
Step 6: But now we want to call child routing from department button clicking.
navigateToDepartment(){
[Link](['department','dept-child']);
}
Step 7: add <router-outlet> tag parent component i.e department-list component.
If we remove <router-outlet> tag from parent component then
See the URL, its [Link] but only department-list works!! will
be displayed, not department-child works!!
Step 8: If we don’t want same time both components will load then, we have to do some mdifications
as follows,
Modify the routes in [Link]
Add one button in [Link]
Create object of router in constructor and add method as,
Click on Department button
Click on Department-child button
Step 9: Same, we want an again child component of department-child component, means
grandchild then,
Create one component as department-grand-child
Add child routing in dept-child routing
Create button in department-child component
Step 10: If we want a one more child for department-list component
Add one more button in
Define the method,
navigateToDepartmentSecondChild(){
[Link](['department','dept-child1']);
}