【发布时间】:2017-10-26 06:45:55
【问题描述】:
为了我的一生,我无法完成这项工作。
我搜索了再搜索,但找不到任何示例(所有示例都带有.fromEvent(),http.get 上没有)。
在我的模板中,我有这个输入:
<input type="text" (input)="categoriesSearch($event)">
在我的组件中,我有以下内容:
categoriesSearch(event) {
this.categoriesSubscription = this.apiService
.getCategoriesList(this.uploadForm.get('categories').value)
.debounceTime(3000)
// .throttleTime(3000)
.subscribe(
(response) => {
this.categories = response.data;
}
);
}
这是我的 ApiService 中的方法:
getCategoriesList(keyword = null) {
const headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Bearer', this.authService.user.token);
const getParams: URLSearchParams = new URLSearchParams();
getParams.set('keyword', keyword);
return this.http.get(this.apiHost + '/categories', { headers: headers, search: getParams })
.map(response => response.json());
}
在categoriesSearch()方法中,debounceTime()和throttleTime()都试过了(我也导入了,当然import 'rxjs/add/operator/debounceTime',import 'rxjs/add/operator/throttleTime')。
但是 http.get 请求根本没有去抖动或限制!如果我在 3 秒内输入 10 个字符,它会发出 10 个 http 请求。
我到底如何告诉this.http.get 仅在自上次请求或“无请求”(即最初的 3 秒延迟)以来至少 3 秒后才发出请求?好的,也许在这里我应该说“自从我上次在我的输入中输入了一些东西”。
我也尝试过直接在服务中使用 debounceTime()/throttleTime(),在 .map() 运算符之前 - 但结果是一样的。
【问题讨论】:
-
我认为
delay是您正在寻找的(我也是)learnrxjs.io/operators/utility/delay.html