订阅switchMap RXJS

时间:2019-06-05 09:42:51

标签: angular rxjs

使用以下代码遇到2个语法错误:

handleTypeahead = (text$: Observable<string>) => {
    text$.pipe(
      distinctUntilChanged(),
      debounceTime(500),
      .tap((term) => this.target = text$),
    // tap((term) => console.log("handleTypeahead", term)),
    // tap((term) => this.onTypeahead.emit(term)),
    /*map((term: string) => [])*/
    /*map((term: string) => term === '' ? [] : this.data)*/
    .switchMap(term => term === '' ? [] : this.data)
  ).subscribe((term) => this.onTypeahead.emit(term))

  }

1)by debounceTime-“期望表达”。 2)通过.subscribe-“无法解析的函数或方法subscription()” 我在没有pipe运算符的情况下尝试了此操作,但是还存在语法错误,这对于RXJS来说是很新的,但是在正确使用此方法时存在很多问题。 v2:

handleTypeahead = (text$: Observable<string>) => {
    text$
      distinctUntilChanged(),
    debounceTime(500),
      .tap((term) => this.target = text$),
    // tap((term) => console.log("handleTypeahead", term)),
    // tap((term) => this.onTypeahead.emit(term)),
    /*map((term: string) => [])*/
    /*map((term: string) => term === '' ? [] : this.data)*/
    .switchMap(term => term === '' ? [] : this.data)
      .subscribe((term) => this.onTypeahead.emit(term))
  }

在这里,我得到的只是错误编号2,感谢任何帮助(当然会投票赞成)

1 个答案:

答案 0 :(得分:0)

删除.tap.switchMap之前的点应该可以:

text$.pipe(
  distinctUntilChanged(),
  debounceTime(500),
  tap(term => this.target = text$),
  switchMap(term => term === '' ? [] : this.data)
).subscribe(term => this.onTypeahead.emit(term));