简体   繁体   English

嵌套子路由模块不适用于 angular 9

[英]Nested Child Route Module is not working with angular 9

I have defined nested route like below.我已经定义了如下的嵌套路由。

home --
       about --
                test

when click on link host/about , its working.当点击链接host/about时,它的工作。 But when i navigate to host/about/test , its not working, just redirecting to "/".但是当我导航到host/about/test时,它不起作用,只是重定向到“/”。

Please find the code below and demo at stackblitz .请在stackblitz找到下面的代码和演示。 and help me with the issue.并帮助我解决这个问题。

app.module.ts
const appRoutes: any = [
  { path: 'about', pathMatch: 'full', loadChildren: './about/about.module#AboutModule' },
];

@NgModule({
  imports:      [ BrowserModule, FormsModule, RouterModule.forRoot(
      appRoutes ) ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { 


}
app.component.html

<a href="/about">Go to  /about</a><br>

<a href="about/test">Go to  /about/test</a>

<router-outlet></router-outlet>

about.module.ts
const appRoutes: Routes = [
  { path: '', pathMatch: 'full', component: AboutComponent },
  { path: 'test', pathMatch: 'full', loadChildren: './test/test.module#TestModule' },
];

@NgModule({
  declarations: [
    AboutComponent
  ],
  imports: [
    CommonModule,
    RouterModule.forChild(appRoutes)
  ],
  providers: []
})

------------
test.module.ts

const appRoutes: Routes = [
  { path: '', pathMatch: 'full', component: TestComponent }
];

@NgModule({
  declarations: [
    TestComponent
  ],
  imports: [
    CommonModule,
    RouterModule.forChild(appRoutes)
  ],
  providers: []
})

I went through your demo in stackblitz and found out two issues:我在 stackblitz 中浏览了你的演示,发现了两个问题:

  • you shouldn't use pathMatch: 'full' for paths with loadChildren configuration.您不应该将 pathMatch: 'full' 用于具有 loadChildren 配置的路径。

app.module.ts app.module.ts

{ path: 'about', pathMatch: 'full',  loadChildren: './about/about.module#AboutModule' 
                 ^^^^^^^^^^^^^^^^^
                   remove this
  • pay attention on spelling注意拼写

test.module.ts测试模块.ts

export class TesttModule {
                ^^
              doubled 't'

Forked Stackblitz 分叉的 Stackblitz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM