简体   繁体   English

在 Angular 中使用反应式方法 RXJS 时如何在合并扫描中调用服务

[英]How to call a SERVICE inside a Merge Scan when using the Reactive Approach RXJS in Angular

I am trying to use the reactive approach in my angular app and I am having trouble calling a service inside a scan.我正在尝试在我的 Angular 应用程序中使用反应式方法,但我无法在扫描中调用服务。 How do I do this?我该怎么做呢? It seems the service returns an observable - how do I deal with this?似乎服务返回了一个可观察的 - 我该如何处理? Here is the error that I see: Type 'Observable' is not assignable to type 'SomeItemModel'.这是我看到的错误:类型“Observable”不可分配给类型“SomeItemModel”。

itemsWithAdditions$ = merge(this.items$, this.saveItemAction$).pipe(
scan((acc, value) => {
  //Check if an array
  if (value instanceof Array) {

    return this.someService.someMethod(res).pipe(map(res => {
      value.forEach(item => {
        item.price = res.find(x => x.id === item.id).map(x => x.price);
      })
        
    }))
  }
  // Otherwise, add that value to the array
  else {
    acc.push(value);
    return acc;
  }
}, [] as SomeItemModel[] // this is the initial value for the array
)

); );

I your case, you need to use mergeScan if you want to return an observable in a scan .在你的情况下,如果你想在scan中返回一个 observable,你需要使用mergeScan This means, any returned value must be an observable :这意味着,任何返回值都必须是可观察的:

itemsWithAdditions$ = merge(this.items$, this.saveItemAction$).pipe(
  mergeScan(
    (acc, value) => {
      if (value instanceof Array) {
        return this.someService.someMethod(res).pipe( // observable here 
          map((res) => {
            // don't forget to return a value here
            value.forEach((item) => {
              item.price = res
                .find((x) => x.id === item.id)
                .map((x) => x.price);
            });
          })
        );
      }
      else {
        acc.push(value);
        return of(acc); // observable here
      }
    },
    [] as SomeItemModel[] 
  )
);

NB: Don't forget to return a value in a map() .注意:不要忘记在map()中返回一个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何做一个简单的 angular Jasmine 单元测试时使用 RXJS 使用主题的反应方法和调用服务的 stream - How to do a simple angular Jasmine Unit testing when using RXJS reactive approach using a subject and a stream that calls a service 在 Angular 中使用带有 RxJS 的声明式/反应式数据访问方法时如何“传递”数据? - How to “pass” data when using a declarative/reactive data access approach with RxJS in Angular? rxjs/angular - 如何合并来自服务的规范化数据? - rxjs/angular - how to merge normalized data from service? 使用 RxJS 弹珠测试 Angular 反应性 Forms - Test Angular Reactive Forms using RxJS Marbles 如何在 rxjs retryWhen 方法中使用角度服务 - how to use angular service inside rxjs retryWhen method 带有异步 RxJS 调用的 Angular 反应表单自定义验证器 - Angular reactive forms custom validator with an async RxJS call RxJs 合并算子获取最新数据并使用扫描算子进行累积 - RxJs merge operator to get latest data and accumulate using scan operator 如何在角度4反应式方法中使用或链接类 - how to use or link classes in angular 4 reactive approach Angular 如何调用基本 Class 方法中的服务,如果它是正确的方法? - Angular how to call service in base Class method, if its the right approach? 被动式Angular2 - Reactive approach Angular2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM