【问题标题】:Get values from observable other with subscribe() methode使用 subscribe() 方法从 observable other 获取值
【发布时间】:2019-02-19 17:08:31
【问题描述】:

我有一个检索值的函数,在其中我使用 observable。

this.values$ = this.apiService.retrieveIndicatorHistory(this.indicatorName, this.currentPeriod)
  .pipe(
    tap(_res => console.log(`value: ${_res}`)),
    map(
      (_res: any) => {
      this.indicatorService.changeLoadingIndicator(this.indicatorName, false);
        return _res;
      }
    ),
    catchError((err: any) => {
      this.indicatorService.changeLoadingIndicator(this.indicatorName, false);
      return of(err);
    })

在同一个文件中,我有一个函数需要 observable 返回的值。

 this.valuesCopy = _.cloneDeep(this.values$);

我收到此错误

error TS2322: Type 'Observable<any>' is not assignable to type 'any[]'.

我知道我应该订阅可观察的检索值(在 retrieveIndicatorHistory 函数内)

我想知道 TS 文件中是否有 this.values$ | async 等效项?

【问题讨论】:

  • 我们在 TS 文件中没有任何异步等效项。您需要使用 subscribe 才能读取可观察值并对其应用 deepClone。
  • 你可以只使用 Promise。 async 管道仍然可以工作,您可以在 js 中使用 await 结果。 obs$.pipe(toPromise())

标签: javascript angular typescript asynchronous observable


【解决方案1】:

请分享完整的文件和 package.json。 这显然是一个类型问题。您只能在 Typescript 中分配类似类型的值。

但是这个问题的答案可以在这里找到。 Type 'Observable<any>' is not assignable to type '[]'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多