在Angular 2+中一个接一个地执行解析器 [英] Executing resolvers one after the other in Angular 2+
问题描述
给出以下路线:
path: '',
component: MyComponent,
resolve: {
foo: FooResolver,
bar: BarResolver
}
有没有办法告诉angular执行第一个旋转变压器FooResolver
,并且仅在第一个旋转变压器完成后才执行第二个旋转变压器BarResolver
?
Is there any way of telling angular to execute the first resolver FooResolver
, and only execute the second resolver BarResolver
once the first one has finished?
推荐答案
解析器是并行解析的.如果应该依次解析Foo
和Bar
,则它们应该是单个FooBar
解析器.如果应该将它们自己用于其他路由,则FooBar
可以包装Foo
和Bar
解析器:
Resolvers are resolved in parallel. If Foo
and Bar
are supposed to be resolved in series they should be a single FooBar
resolver. If they are supposed to be used by themselves in other routes, FooBar
can wrap Foo
and Bar
resolvers:
class FooBarResolver implements Resolve<{ foo: any, bar: any }> {
constructor(
protected fooResolver: FooResolver,
protected barResolver: BarResolver
) {}
async resolve(route): Promise<{ foo: any, bar: any }> {
const foo = await this.fooResolver.resolve(route);
const bar = await this.barResolver.resolve(route);
return { foo, bar };
}
}
FooBar
应该注意以下事实:从Foo
和Bar
返回的是应许还是可观察到的,以便正确地解决它们.否则,应添加其他安全装置,例如await Observable.from(this.fooResolver.resolve(route)).toPromise()
.
FooBar
should be aware of the fact if it is a promise or an observable that is returned from Foo
and Bar
in order to resolve them properly. Otherwise additional safety device should be added, like await Observable.from(this.fooResolver.resolve(route)).toPromise()
.
FooBar
和Foo
或Bar
不应出现在同一路径中,因为这将导致重复的分辨率.
FooBar
and Foo
or Bar
shouldn't appear within same route because this will result in duplicate resolutions.
这篇关于在Angular 2+中一个接一个地执行解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!