angular 路由器解析器 - 重新加载时从父级获取参数
angular router resolver - get params from parent on reload
我有一个这样的路由,有一个子路由的解析器。
{
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
获取所选组织。
现在当我重新加载路线时
/organizations/12345/profile
我希望解析器检查是否存储了 activeOrganization
。如果不是,它应该通过 http.get
和 12345
作为 id
.
检索它
我的问题是我不知道使用哪个工具可以检索参数。通常在组件中我可以简单地使用 activatedRoute.parent.params
但在这种情况下,注入的 activatedRoute.parent
是 null
.
我想既然我已经在 root 中提供了解析器,parent=null
是有意义的,但是注销快照我在父级中得到 null
以及 firstChild
...
ActivatedRouteSnapshot Contains the information about a route
associated with a component loaded in an outlet at a particular moment
in time. ActivatedRouteSnapshot can also be used to traverse the
router state tree.
import { Resolve, ActivatedRouteSnapshot } from '@angular/router';
resolve(route: ActivatedRouteSnapshot) {
let id: any = route.params['id'];
}
我有一个这样的路由,有一个子路由的解析器。
{
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
获取所选组织。
现在当我重新加载路线时
/organizations/12345/profile
我希望解析器检查是否存储了 activeOrganization
。如果不是,它应该通过 http.get
和 12345
作为 id
.
我的问题是我不知道使用哪个工具可以检索参数。通常在组件中我可以简单地使用 activatedRoute.parent.params
但在这种情况下,注入的 activatedRoute.parent
是 null
.
我想既然我已经在 root 中提供了解析器,parent=null
是有意义的,但是注销快照我在父级中得到 null
以及 firstChild
...
ActivatedRouteSnapshot Contains the information about a route associated with a component loaded in an outlet at a particular moment in time. ActivatedRouteSnapshot can also be used to traverse the router state tree.
import { Resolve, ActivatedRouteSnapshot } from '@angular/router';
resolve(route: ActivatedRouteSnapshot) {
let id: any = route.params['id'];
}