问题描述
给定以下路线:
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
返回的 promise 还是 observable 以便解析他们正确.否则应该添加额外的安全设备,比如 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+ 中一个接一个地执行解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!