Search code examples
javascriptangularangular-ui-router

Reloading Angular children component takes user to localhost:4200


In my Angular application, I have some children components that may take route parameters. I can redirect to these children components from parent easily but when I reload the children component, application navigates to localhost:4200 and gives the routing error as below.

Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'project-setup/project-setup-steppers'

Here is my routing configurations:

const routes: Routes = [
  { path: 'dashboard', component: DashboardComponent, canActivate: [AuthenticationGuardService] },
  { path: 'project-setup', component: ProjectSetupComponent, canActivate: [AuthenticationGuardService] ,
    children: [
    {
      path: 'project-setup-steppers/:projectId',
      component: ProjectSetupSteppersComponent,
      canActivate: [AuthenticationGuardService]
    },
    {
      path: 'selfs', component: SelfsComponent, canActivate: [AuthenticationGuardService],
      children: [
        {
          path: 'raters/:selfUserId/:formSetUpKey',
          component: RatersComponent, canActivate: [AuthenticationGuardService]
        }
      ]
    },
  ]},
  { path: 'participant-setup', component: ParticipantSetupComponent, canActivate: [AuthenticationGuardService] },
  { path: 'launch-and-status', component: LaunchStatusComponent, canActivate: [AuthenticationGuardService] },
  { path: 'launch-project-desc', component: LaunchProjectDescriptionComponent, canActivate: [AuthenticationGuardService] },
  { path: 'question-setup', component: QuestionSetupComponent, canActivate: [AuthenticationGuardService] },
  { path: 'email-setup', component: EmailSetupComponent, canActivate: [AuthenticationGuardService] },
  { path: 'participant-steppers', component: ParticipantSteppersComponent, canActivate: [AuthenticationGuardService], children: [] },
  {
    path: 'report-project-list', component: ReportProjectListComponent, canActivate: [AuthenticationGuardService],
    children: [
      {
        path: 'report-project-detail-list/:projectId',
        component: ReportProjectDetailListComponent, canActivate: [AuthenticationGuardService]
      }
    ]
  },

  { path: 'report-detail/:id/:type', component: ReportDetailComponent, canActivate: [AuthenticationGuardService] },
  { path: 'trial/:projectId', component: TrailRunProjectComponent, canActivate: [AuthenticationGuardService] },
  { path: 'status-of-project/:projectId', component: StatusOfProjectComponent, canActivate: [AuthenticationGuardService] },
  { path: 'project-status-details', component: ProjectStatusDetailsComponent, canActivate: [AuthenticationGuardService] },
  { path: 'news', component: NewsComponent, canActivate: [AuthenticationGuardService] },
  { path: 'view-responses/:assigneeId', component: ViewProjectResponsesComponent, canActivate: [AuthenticationGuardService] },
  { path: 'copy-project/:projectId', component: CopyProjectComponent, canActivate: [AuthenticationGuardService] }

];

I am using nested router-outlet in parent components to render children component. I want to stay at the same route even if user reloads the application from a child component.


Solution

  • Add this to your app-routing file

          ...
          },
          {
            path: 'report-project-detail-list',
            component: ReportProjectDetailListComponent, canActivate: [AuthenticationGuardService]
          }
    

    You must define the route with and without the param if you need it optionnal.