英文:
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
Type '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 'rxjs';
import { take, map, toArray } from 'rxjs/operators';
from(emissionArray)
.pipe(
take(emissionArray.length),
interval(1000), //Error is thrown by interval
map((i) => emissionArray[i]),
toArray()
)
.subscribe((values) => 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(() => 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]) => emissionArray.length),
map(([emissionArray, i]) => emissionArray[i])
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论