angular-debounceTime 防抖

1.新建一个Subject对象;

private searchUserNick = new Subject<string>();

2.在需要的地方调用这个对象

this.searchUserNick.next(val);

3.在ngOnInit中初始化

  ngOnInit() {
    this.initSearchUserSubscription();
  }

//防抖函数请求
  private initSearchUserSubscription() {
    this.searchUserNick.pipe(
      debounceTime(400),
      distinctUntilChanged())
      .subscribe((searchContent) => {
        //searchContent 为获取的参数
       // 400后 发送请求
        this.req(searchContent); // 请求函数
        });
      });
  }
原文地址:https://www.cnblogs.com/huangmin1992/p/13292934.html