繁体   English   中英

带有子路由的 Angular 在父组件下方加载子组件,而不是将其重定向到另一个视图

[英]Angular with child routes load child components below parent component instead of redirecting it to another view

我有一个 MuscleComponent,它有 MuscleAddComponent 和 MuscleEditComponent 作为它的子组件。 当我点击它们时,我试图重定向到它的子组件,但它们出现在 MuscleComponent 的同一视图中。

注意:当我有{ path: '', component: MusclesComponent, pathMatch: 'full' },在肌肉路​​径的子项中,我在同一页面上得到两个相同外观的 MuscleComponent 的 html。 为什么会这样? 为什么当我单击“添加”按钮时,它会在 MuscleComponent 的 html 上显示 MuscleAddComponent 的内容?

app-routing.module.ts

const routes: Routes = [
  { path: '', component: HomeComponent, pathMatch: 'full' },
  {
    path: 'muscles', component: MusclesComponent, children: [
      { path: '', component: MusclesComponent, pathMatch: 'full' },
      { path: 'add', component: MuscleAddComponent },
      { path: ':id', component: MuscleEditComponent }
    ]
  },
  { path: 'workouts', component: WorkoutsComponent },
  { path: 'myplanner', component: MyPlannerComponent },
  { path: '**', redirectTo: '' }
];

应用程序组件.html

<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <a class="navbar-brand" href="#">
        <img src="../assets/images/logo.png" height="60px" style="padding: 0px; margin: 0px;">
    </a>
    <app-menus></app-menus>
</nav>
<router-outlet></router-outlet>

肌肉.component.html

<div class="container-fluid">
    <div class="row" style="padding: 25px;">
        <div class="col-md-6">
            <h2>Manage Muscles</h2>
        </div>
        <div class="col-md-6">
            <div class="float-right">
                <button type="button" class="btn btn-secondary" [routerLink]="['./add']">Add New</button>
            </div>
        </div>
    </div>
</div>

<router-outlet></router-outlet>

任何帮助都受到高度赞赏。

尝试使用forChildmuscles.module.ts

RouterModule.forChild([
{
    path: 'muscles', component: MusclesComponent, children: [
      { path: '', component: MusclesComponent, pathMatch: 'full' },
      { path: 'add', component: MuscleAddComponent },
      { path: ':id', component: MuscleEditComponent }
    ]
  },
])

或者,如果您没有单独的模块muscles.module.ts

const routes: Routes = [
  { path: '', component: HomeComponent, pathMatch: 'full' },
  { path: 'muscles', component: MusclesComponent, pathMatch: 'full'}
  { path: 'muscles/add', component: MuscleAddComponent },
  { path: 'muscles/:id', component: MuscleEditComponent },
  { path: 'workouts', component: WorkoutsComponent },
  { path: 'myplanner', component: MyPlannerComponent },
  { path: '**', redirectTo: '' }
];

您的路线工作不正确,因为您的组件(例如MuscleAddComponentMuscleEditComponent实际上不是子组件)。 我的意思是它们是独立的组件,因为它们没有一些共享模块。 所以你不必使用children财产。 作为 Angular 文档的一个例子

src/app/crisis-center/crisis-center-routing.module.ts(路由):

const crisisCenterRoutes: Routes = [
  {
    path: 'crisis-center',
    component: CrisisCenterComponent,
    children: [
      {
        path: '',
        component: CrisisListComponent,
        children: [
          {
            path: ':id',
            component: CrisisDetailComponent
          },
          {
            path: '',
            component: CrisisCenterHomeComponent
          }
        ]
      }
    ]
  }
];

请注意,上述路由是在单独的模块.../crisis-center-routing.module.ts

const routes: Routes = [
  { path: '', component: HomeComponent, pathMatch: 'full' },
  {
    path: 'muscles', children: [
      { path: '', component: MusclesComponent },
      { path: 'add', component: MuscleAddComponent },
      { path: ':id', component: MuscleEditComponent }
    ]
  },
  { path: 'workouts', component: WorkoutsComponent },
  { path: 'myplanner', component: MyPlannerComponent },
  { path: '**', redirectTo: '' }
];

如果您从muscle.html 中删除路由器插座,这条路线应该可以工作

暂无
暂无

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

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