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

查看:29
本文介绍了在 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 返回的 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().

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

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

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

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