Angular 5在方法上使用debounceTime

cel*_*ade 2 rxjs angular

我正在尝试在Angular 5函数上使用debounceTime,但不确定如何使用它。构建搜索功能时,可以使用它,因为它绑定到对该输入值所做的更改,如下所示:

this.search
    .debounceTime(400)
    .distinctUntilChanged()
    .takeUntil(this.onDestroy)
    .subscribe((model) => {
        return this.filterArray(model);
    });
Run Code Online (Sandbox Code Playgroud)

但是现在我想将其应用于一个函数,该函数在很多地方都被调用过,并通过http post将事件发送到数据库,如下所示:

private saveData(): void {
    this.http.post('event/url', data).takeUntil(this.onDestroy).subscribe((response: Response) => {
        // -
    }, error => {
        // -
    });
}
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?saveData().debounceTime()还是我需要以其他方式做到这一点?

Rob*_*hof 5

我不确定,但是类似的方法可能有效:

$save: Subject<void> = new Subject<void>();
Run Code Online (Sandbox Code Playgroud)

在您的构造函数或init中:

this.$save
  .debounceTime(400)
  .switchMap(() => this.http.post(...post params))
  .subscribe(..Do something);
Run Code Online (Sandbox Code Playgroud)

您的保存方法:

saveData(){
    this.$save.next();
}
Run Code Online (Sandbox Code Playgroud)