在Angular 2+中一个接一个地执行解析器 [英] Executing resolvers one after the other in Angular 2+

查看:68
本文介绍了在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?

推荐答案

解析器是并行解析的.如果应该依次解析FooBar,则它们应该是单个FooBar解析器.如果应该将它们自己用于其他路由,则FooBar可以包装FooBar解析器:

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应该注意以下事实:从FooBar返回的是应许还是可观察到的,以便正确地解决它们.否则,应添加其他安全装置,例如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().

FooBarFooBar不应出现在同一路径中,因为这将导致重复的分辨率.

FooBar and Foo or Bar shouldn't appear within same route because this will result in duplicate resolutions.

这篇关于在Angular 2+中一个接一个地执行解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆