0

我有这个 JSON 文件,其中包含以下作业:

{
  "jobs": [
    {
      "id": 0,
      "name": "Web Dev",
      "price": 200,
      "subtypes": [
        {
          "id": 0,
          "name": "Client Side",
          "price": 49.99
        },
        {
          "id": 1,
          "name": "Server Side",
          "price": 49.99
        },
        {
          "id": 2,
          "name": "SEO",
          "price": 49.99
        }
      ]
    }
}

我的路线是这样的:

export const routes: Routes = [
  { path: 'menu', component: MenuComponent },
  { path: 'menu/:id', component: JobDetailComponent },
  { path: 'menu/:id/:subtypes/:id', component: ItemDetailComponent }
]

菜单正确显示此代码中的所有作业:

    <div fxFlex *ngIf="jobs">
      <mat-grid-list cols="3" rowHeight="200px" >
        <mat-grid-tile *ngFor="let job of jobs" [routerLink]="['/menu', job.id]">
          <h1>{{job.name | uppercase}}</h1>
        </mat-grid-tile>
      </mat-grid-list>
    </div>

单击“mat-grid-tile”时,代码正确路由到作业子类型页面,如​​下所示:

  <div fxFlex *ngIf="job.subtypes">
    <div *ngFor="let stp of job.subtypes" [routerLink]="['/menu', stp.id, '/subtypes', stp.id]">
        <h1>{{stp.name}}</h1>
    </div>
  </div>

但是,我无法让路由器链接在单击时重定向到所需的子类型页面。

子类型页面如下:

<div *ngIf="job.subtypes">

  <h1>
    {{job.name}}
  <h1>
</div>

而且它没有在单击的子类型链接上显示所需的信息

除了我的代码不正确之外,我认为它是不正确的;使用嵌套信息的事实是 JSON-serve api 用于测试我的开发应用程序的问题

4

1 回答 1

0

您应该使用子路由。Angular 文档 - https://angular.io/guide/router#child-routing-component 这里有一个很好的例子:https ://angular-2-training-book.rangle.io/routing/child_routes

注意 - 这意味着子路由只能从父路由访问(例如,只能从 /menu/...)

于 2020-01-26T06:29:47.297 回答