Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 5 child routing

I have a problem regarding the routing in Angular 5. I would like to create a child route but when i type the url (for child route) the parent component gets loaded. My routing:

path: 'user-admin/:id',
component: UserAdminComponent,
},
children: [
  {
    path: '',
    component: UserAdminComponent,
    pathMatch: 'full'
  },
  {
    path: "final-exams",
    component: FinalExamsComponent,
  },

The two routes that belongs to the problem:

http://localhost:4200/user-admin/0

http://localhost:4200/user-admin/0/final-exams

Thank you for your help in advance! :)

like image 716
user2102355 Avatar asked Aug 30 '26 07:08

user2102355


1 Answers

Have a look at the working solution

Stack Blitz, Source Code

user-admin/1 // Hello will be printed

user-admin/1/final-exams // Hola will be the output

Problem was:

You mentioned the same component in both Parent and Child Route & Children routes were not mentioned inside the parent route

const appRoutes: Routes = [
  {
    path: 'user-admin/:id',
    // component: HelloComponent, // No need to mention the same component, in parent
    children: [     // Children routes are inside the parent route
      {
        path: '',
        component: HelloComponent,
        pathMatch: 'full'
      },
      {
        path: "final-exams",
        component: HolaComponent
      }
    ]
  }
];
like image 147
Abhijit Kar ツ Avatar answered Sep 01 '26 04:09

Abhijit Kar ツ