简体   繁体   English

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

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

I have a MuscleComponent which has MuscleAddComponent and MuscleEditComponent as its child components.我有一个 MuscleComponent,它有 MuscleAddComponent 和 MuscleEditComponent 作为它的子组件。 I am trying to redirect to its child components when I click on them but instead they appear on the same view of MuscleComponent.当我点击它们时,我试图重定向到它的子组件,但它们出现在 MuscleComponent 的同一视图中。

Note: When I have { path: '', component: MusclesComponent, pathMatch: 'full' }, in the children of muscle path I get two same appearance of MuscleComponent's html on same page.注意:当我有{ path: '', component: MusclesComponent, pathMatch: 'full' },在肌肉路​​径的子项中,我在同一页面上得到两个相同外观的 MuscleComponent 的 html。 Why is it so?为什么会这样? and Why when I click on Add button it shows the content of MuscleAddComponent on MuscleComponent's html?为什么当我单击“添加”按钮时,它会在 MuscleComponent 的 html 上显示 MuscleAddComponent 的内容?

app-routing.module.ts 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: '' }
];

app.component.html应用程序组件.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>

muscles.component.html肌肉.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>

Any help is highly appreciated.任何帮助都受到高度赞赏。

Try to use forChild in your muscles.module.ts :尝试使用forChildmuscles.module.ts

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

or if you don't have separate module muscles.module.ts :或者,如果您没有单独的模块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: '' }
];

Your routes were working incorrectly as your components such as MuscleAddComponent , MuscleEditComponent are not actually children.您的路线工作不正确,因为您的组件(例如MuscleAddComponentMuscleEditComponent实际上不是子组件)。 I mean they are separate components, because they do not have some shared module.我的意思是它们是独立的组件,因为它们没有一些共享模块。 So you don't have to use children property.所以你不必使用children财产。 As an example from Angular docs :作为 Angular 文档的一个例子

src/app/crisis-center/crisis-center-routing.module.ts (Routes): 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
          }
        ]
      }
    ]
  }
];

Pay attention that the above route is created in separate module .../crisis-center-routing.module.ts .请注意,上述路由是在单独的模块.../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: '' }
];

if you remove the router-outlet from muscles.html this route should work如果您从muscle.html 中删除路由器插座,这条路线应该可以工作

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

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