0

我有一条这样的路线,带有一个子路线的解析器。

 {
        path: 'organizations/:oid',
        component: AdminOrganizationComponent,
        children: [
          {
            path: 'profile',
            component: AdminOrganizationProfileComponent,
            resolve: { organization: OrganizationResolver }
          },
          { path: '', redirectTo: 'profile', pathMatch: 'full' }
        ]
      },

解析器看起来像这样

@Injectable()
export class OrganizationResolver implements Resolve<Observable<Organization>> {
  private activeOrganization: Organization;
  constructor(private route:ActivatedRoute) {}

  setUser(organization: Organization) {
    this.activeOrganization = organization;
  }

  resolve() {
    return of(this.activeOrganization);
  }
}

在导航到路线之前,我使用该服务来设置所选组织。

  edit(organization) {
    this.organizationResolver.setUser(organization);
    this.router.navigate(['/admin/organizations', organization.id]);
  }

到目前为止,它按预期工作,我可以使用它snapshot.data来获取选定的组织。

现在当我重新加载路线时

/组织/12345/个人资料

我希望解析器检查是否有activeOrganization存储。如果没有,它应该通过http.get带有12345as的 a 来检索它id

我的问题是我不知道用哪个工具可以检索参数。通常在一个组件中我可以简单地使用activatedRoute.parent.params 但在这种情况下注入activatedRoute.parent的是null.

我想,既然我已经在 root 中提供了解析器,那parent=null是有道理的,但是注销我null在父级中获得的快照以及firstChild......

4

1 回答 1

1

ActivatedRouteSnapshot 包含有关与在特定时刻加载到插座中的组件关联的路由的信息。ActivatedRouteSnapshot 也可以用来遍历路由器状态树。

import { Resolve, ActivatedRouteSnapshot } from '@angular/router';

  resolve(route: ActivatedRouteSnapshot) {
    let id: any = route.params['id'];
  }
于 2018-08-21T11:46:58.927 回答