【发布时间】:2019-02-01 02:41:54
【问题描述】:
我正在尝试将 Angular 5 应用程序迁移到 Angular 6,而我有这一段 RxJS 代码让我很困惑。我已经通过 Google 搜索并阅读了 RxJS 文档,足以知道我应该使用 .pipe,但我无法完全理解语法。
当用户在搜索输入字段中键入内容时,代码会从 API 调用返回结果。新版本目前抛出错误:
ERROR TypeError: Cannot read property 'call' of undefined
at merge.js:8
at MapSubscriber.project (switchMap.js:9)
原码:
this.searchResults =
this.form.controls['search'].valueChanges
.debounceTime(200)
.switchMap(query => this.search.searchPeople(query))
.merge(this.clearSearch.mapTo([]));
新版本:
this.searchResults = this.form.controls['search'].valueChanges.pipe(
debounceTime(200),
switchMap(query => this.search.searchPeople(String(query)),
merge(this.clearSearch.pipe(mapTo([])))));
我已经像这样导入了 RxJS:
import { Observable } from 'rxjs';
import { Subject } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
import { mapTo } from 'rxjs/operators';
import { merge } from 'rxjs/operators';
import { switchMap } from 'rxjs/operators';
【问题讨论】:
-
我觉得很好,你确定这个错误来自你的这部分代码吗?
-
@martin 你是对的,代码没有任何问题。 API 调用的格式略有变化。
标签: angular typescript rxjs