【问题标题】:Where to load resource from routes like /page/:id/subpage etc从 /page/:id/subpage 等路由加载资源的位置
【发布时间】:2019-07-19 03:08:27
【问题描述】:

我目前的应用组件如下所示:

<app-navigation></app-navigation>
<router-outlet></router-outlet>

和路线:

const appRoutes: Routes = [
  { path: 'items', component: ListComponent },
  { path: 'items/:id', component: DetailsComponent },
  { path: 'items/:id/page1', component: Page1Component },
  { path: 'items/:id/page2', component: Page2Component },
  { path: 'anotherpage', component AnotherPageComponent} },
];

id 参数是资源的 ID,我使用 http 服务加载,它对所有子页面都有效。这意味着,每次用户从 Page1 导航到 Page2 时,我都不需要加载它。

现在的问题是,在哪里加载资源?

目前正在做DetailsComponent:

export class DetailsComponent {

  isLoading = true;

  constructor(
    private backend: BackendService,
    protected state: StateService,
    private route: ActivatedRoute) {

    this.route.params.pipe(
      map(params => params['id']),
      filter(id => this.state.currentItem != id),
      distinct(),
      tap(() => {
        this.isLoading = true;
        this.state.currentCase = null
      }),
      switchMap(id => backend.getItemById(id)),
      tap(() => this.isLoading = false)
    ).subscribe(response => {
      this.state.currentCase = response;
    });
  }
}

我想在每个页面(Page1,Page2)等中都这样做不是最好的主意。

我想我可以在router-outlet 内的“ItemContainerCompoent”中有另一个router-outlet,但是当用户在内部router-outlet 中的页面之间导航时,我将如何突出显示链接

【问题讨论】:

    标签: angular typescript angular7 angular-router


    【解决方案1】:

    你需要的是 child 路线:

    const appRoutes: Routes = [
      { path: 'items', component: ListComponent },
      { path: 'items/:id', component: DetailsComponent 
        children: [
           { path: 'page1', component: Page1Component },
           { path: 'page2', component: Page2Component },
           { path: 'anotherpage', component AnotherPageComponent} }
        ]
      }
    ];
    

    此文档对您有用:Milestone 4: Crisis center feature

    【讨论】:

    • 我仍然不清楚,根据 id 加载资源的位置。在我的问题中,DetailsComponent 和 Page1Component 是专有的。显示一个或另一个.... 我想我必须在 items/:id 路径中创建包含加载逻辑和 &lt;router-outlet&gt; 的父组件,然后将 DetailsComponent 移动到子组件
    • 是的,您需要创建这个父组件。您可以使用 resolve 技术预加载数据,然后通过this.route.parent.data 在子组件中访问它。无论如何,有很多方法可以满足您的需求。路由或子输入...
    猜你喜欢
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    相关资源
    最近更新 更多