我正在尝试使用以下代码节流
ngrx 存储操作更新事件
import 'rxjs/add/operator/throttle'
import { Dispatcher, Store } from '@ngrx/store';
...
static get parameters() {
return [[Dispatcher]];
}
constructor(actions$) {
...
this.actions$
.filter(action => action.type === this.Actions[`LOAD_USERS_REQUEST`])
.throttle(1000 /* ms */)
.subscribe(() =>
...
);
这会抛出一个错误
at ThrottleSubscriber.tryDurationSelector (throttle.js:80) TypeError: this.durationSelector is not a function
当我将 .throttle(1000)
替换为 .throttle(() => 1000)
时,它会抛出一个不同的错误,清楚地表明 throttle 需要一个函数,而不是我提供的那个。但我想知道为什么,因为文档指出 throttle 需要一个数字值。
https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/throttle.md
最佳答案
您在 https://github.com/Reactive-Extensions/RxJS 上引用的文档页面与 RxJS 4 相关。由于您使用的是 Angular2,因此您使用的是 RxJS 5。
接线员 throttle()
期望将 Observable 或 Promise 作为参数。
接线员 throttleTime()
以毫秒为单位作为参数时间。
所以你应该使用throttleTime(1000)
。
请注意,使用 .throttle(() => 1000)
非常不同。您传递一个匿名函数,该函数返回 1000
而不是直接返回 1000
数字。这就是它抛出不同错误的原因。
关于angular - rxjs 限制 this.durationSelector 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40106953/