首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular 5,实现搜索输入

Angular 5,实现搜索输入
EN

Stack Overflow用户
提问于 2021-04-16 18:38:13
回答 1查看 30关注 0票数 0

我正在尝试遵循这个example

但我不明白为什么它不能工作。以下是我的代码

代码语言:javascript
复制
import { Subject } from "rxjs/Subject";
import { Component, OnInit } from "@angular/core";
import { Observable } from "rxjs";
import {
  SearchFullText,
  SearchFullTextService,
} from "../search-full-text.service";
import { debounceTime, distinctUntilChanged, switchMap } from "rxjs/operators";

@Component({
  selector: 'app-header',
  templateUrl: './header.component.html',
  styleUrls: ['./header.component.scss']
})
export class HeaderComponent implements OnInit {
      persons$: Observable<SearchFullText[]>;
      private searchValues = new Subject<string>();

  constructor(private searchService: SearchFullTextService) {}
  // Push a search term into the observable stream.
  search(searchValue: string): void {
    this.searchValues.next(searchValue);
  }

  ngOnInit(): void {
    this.persons$ = this.searchValues.pipe(
      // wait 300ms after each keystroke before considering the term
      debounceTime(300),

      // ignore new term if same as previous term
      distinctUntilChanged(),

      // switch to new search observable each time the term changes
      switchMap((searchValue: string) => {
        console.log("switchMap");
        return this.searchService.searchFullText(searchValue)
     })
    );
  }
}

switchMap未执行。我遗漏了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-16 19:08:41

问题是你没有订阅你的switchMap observable.If,你订阅了它,你的代码就会执行:

代码语言:javascript
复制
switchMap((searchValue: string) => {
    console.log("switchMap");
    return this.searchService.searchFullText(searchValue)
}).subscribe(value => doSomething);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67123702

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档