RXJS interval不接受数字值并抛出语法错误。

huangapple go评论121阅读模式
英文:

RXJS interval won't accept a number value and is throwing a syntax error

问题

以下是您要翻译的部分:

"Simply trying to use Interval in an RXJS chain which is throwing a syntax error when I try to provide 1000 as the parameter. The same thing happens with Timer and the same thing happens if I remove every operator in the pipe except for interval"

"Error"

"Argument of type 'Observable' is not assignable to parameter of type 'OperatorFunction<any, unknown>'.
Type 'Observable' provides no match for the signature '(source: Observable): Observable'."

"I tried to get the operator working and it's throwing a syntax error. The operator takes a parameter of number which I'm providing and it won't accept it"

英文:

Simply trying to use Interval in an RXJS chain which is throwing a syntax error when I try to provide 1000 as the parameter. The same thing happens with Timer and the same thing happens if I remove every operator in the pipe except for interval

import { from, interval } from &#39;rxjs&#39;;
import { take, map, toArray } from &#39;rxjs/operators&#39;;

from(emissionArray)
    .pipe(
      take(emissionArray.length),
      interval(1000), //Error is thrown by interval 
      map((i) =&gt; emissionArray[i]),
      toArray()
    )
    .subscribe((values) =&gt; console.log(values));

Error

Argument of type 'Observable<number>' is not assignable to parameter of type 'OperatorFunction<any, unknown>'.
Type 'Observable<number>' provides no match for the signature '(source: Observable<any>): Observable<unknown>'.

I tried to get the operator working and it's throwing a syntax error. The operator takes a parameter of number which I'm providing and it won't accept it

答案1

得分: 3

interval 不是一个运算符,不应该直接放在管道中使用。

根据您想要做的事情,您可能想使用:

  • delay,等待 xxx 毫秒。
  • switchMap(() => interval(1000)),如果您想要以间隔发射。
  • 其他操作。
英文:

interval is not an operator, it's not meant to be used directly into a pipe.

Depending on what you want to do, you might want to use

  • delay, to wait xxx ms.
  • switchMap(() =&gt; interval(1000)) you want to emit at interval
  • something else.

答案2

得分: 0

Interval 不是一个操作符,它返回一个 observable,你想要结合这两个 observables 使用 combineLatest

combineLatest(from(emissionArray), interval(1000)).pipe(
  take(([emissionArray]) => emissionArray.length),
  map(([emissionArray, i]) => emissionArray[i])
)
英文:

Interval is not an operator it returns an observable, you want to combineLatest the two observables

combineLatest(from(emissionArray), interval(1000)).pipe(
  take(([emissionArray]) =&gt; emissionArray.length),
  map(([emissionArray, i]) =&gt; emissionArray[i])
)

huangapple
  • 本文由 发表于 2023年3月7日 19:08:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661202.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定