subscribe to switchMap 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(按debounceTime - "预期表达"。2(按.subscribe - "未解析的函数或方法订阅(("我在没有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))
  }

在这里,我只得到错误 num#2 任何帮助(当然会投票(

删除.tap.switchMap之前的点应该有效:

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

最新更新