【问题标题】:Angular6 - Child route with Empty Path not loadedAngular6 - 未加载空路径的子路由
【发布时间】:2018-08-20 12:37:38
【问题描述】:

我正在将我的 Angular 4 应用程序转换为 Angular 6。但是,我在进行与路由相关的更改时遇到了问题。

现在,当我运行我的应用程序时,会加载 Home 组件,但没有加载路径为空的子组件 ContentsComponent。

我的应用程序中的路由定义如下:

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>

编辑:请注意,我不想在/home url 部分之后显示任何内容。

【问题讨论】:

标签: angular angular-routing


【解决方案1】:

这是因为相对路径。试试下面应该可以工作。

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

【讨论】:

  • 可行,但我不想在/home url 部分之后显示任何内容。
【解决方案2】:

对于子组件中的路由,您需要提及其名称。

将“/home”替换为“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 { }

在 HomeRoutingModule 中,您无需再次提及“家”。

仅按以下方式命名子组件。

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

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

export class HomeRoutingModule { }

而如果不想在路由的时候改变路由路径,那么有两种选择

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

或

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

如果有任何疑问,请询问我。 谢谢。

【讨论】:

  • 请查看我对问题的编辑。我不想命名ContentsComponent 路线。
  • 如果您不想通过导航影响 url,那么您将不得不使用 'skipLocationChange: true'。看看angular.io/api/router/NavigationExtras
猜你喜欢
  • 2019-05-03
  • 1970-01-01
  • 2014-09-08
  • 2020-05-31
  • 2018-04-23
  • 1970-01-01
  • 2017-07-13
  • 2021-11-06
  • 2020-09-22
相关资源
最近更新 更多