angular - rxjs 限制 this.durationSelector 不是函数

标签 angular typescript rxjs rxjs5 ngrx

我正在尝试使用以下代码节流 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/

相关文章:

rxjs - 选择哪个 RxJS 操作符来处理 HTTP 错误 : tap or catchError?

javascript - Angular 4 - 可观察订阅中对 'this' 的引用似乎引用了旧数据

node.js - 在 Heroku 上构建 Angular 4 应用程序时,不包括 Assets

http - 如何在 Response header Angular2 中获取值

Angular 2 如何创建嵌套结构?

javascript - PhpStorm 2019.2 : Material UI components requiring props which are not required

javascript - 相当于 redux 'getState' 来自 rxjs 中的可观察对象

javascript - Angular 2相同的组件在多个地方具有不同的业务逻辑

Angular 7 无法包含自定义 typescript 定义文件

angular - TS1086 : An accessor cannot be declared in ambient context