6
votes

I am converting my Angular 4 application to Angular 6. However, I am facing issues while making the routing related changes.

Now when I run my application, Home component is loaded but the child component ContentsComponent which has empty path is not loaded.

Routing in my application is defined like below:

app.routing.module.ts

const routes: Routes = [
  { path: '', redirectTo: '/home', pathMatch: 'full' },
  { path: 'home', component: HomeComponent },
  { path: 'support', component: SupportComponent }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})

export class AppRoutingModule { }

home.routing.module.ts

const routes: Routes = [
  {
    path: 'home', component: HomeComponent, 
    children: [
      { path: '', component: ContentsComponent }, //<-- This component is not loaded
      { path: '**', redirectTo: 'home' }
    ]
  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})

export class HomeRoutingModule { }

home.module.ts

@NgModule({
  imports: [
      HomeRoutingModule
      ],
  exports: [],
  declarations: [ HomeComponent, ContentsComponent ],
  providers: [],
})

export class HomeModule { }

app.module.ts

@NgModule({
  imports: [
    BrowserModule,
    AppRoutingModule,
    ReactiveFormsModule,
    HomeModule
  ],
  declarations: [AppComponent],
  bootstrap: [AppComponent],
  providers: [
    { provide: LocationStrategy, useClass: HashLocationStrategy }
  ]
})
export class AppModule { }

home.component.html

<router-outlet></router-outlet>

EDIT: Please note that I don't want to display anything after the /home url part.

2
Your code works fine in stackblitz.com/edit/…Amit Chigadani

2 Answers

0
votes

Its because of relative path. Try below should work.

children: [
      { path: '', redirectTo: '/home/content' }, 
      { path: 'content ', component: ContentsComponent }, 
      { path: '**', redirectTo: 'home' }
    ]
0
votes

For routing in children component you need to mention its name.

Replace '/home' by just 'home'.

const routes: Routes = [
  { path: '', redirectTo: 'home', pathMatch: 'full' },
  { path: 'home', component: HomeComponent },
  { path: 'support', component: SupportComponent }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})

export class AppRoutingModule { }

And in HomeRoutingModule you don't need to mention 'home' again.

Just name children components only as below.

const routes: Routes = [
  {
    path: '', component: HomeComponent, 
    children: [
      { path: 'content', component: ContentsComponent },
      { path: '**', redirectTo: 'content' }
    ]
  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})

export class HomeRoutingModule { }

And If you don't want to change routing path while routing, then there are two options

this.router.navigate(['/home/content'], { skipLocationChange: true });

or

const routes: Routes = [
      {
        path: '', component: HomeComponent, 
        children: [
          { path: '', redirectTo: 'content' }
          { path: 'content', component: ContentsComponent },
          { path: '**', redirectTo: 'content' }
        ]
      }
    ];

Ask me if any query. Thanks.