首页 > 其他 > 详细

【angular7】防抖、截流

时间:2020-01-08 16:39:15      阅读:204      评论:0      收藏:0      [点我收藏+]
...
import { Subject } from ‘rxjs‘;
import { tap, catchError, debounceTime, distinctUntilChanged } from ‘rxjs/operators‘;

...

export class demoComponent implements OnInit {
    //状态
    codeSearch: boolean = false;

    searchText: Subject<string> = new Subject<string>();
  // 事件调用
    searchCode(keyword: string): any {
        this.searchText.next(keyword);
    }
  //调用接口获取数据
    getCode(keyword: string): any {
        if (keyword === ‘‘) return;
        this.codeSearch = true;
        this.http.get(ApiUrl)
            .pipe(tap(() => {this.codeSearch = false;}))
            .subscribe((res: any) => {
                console.log(res)
                if (res.code == 200) {
                    this.strategyList = res.data;
                    this.cdr.detectChanges();
                    if (res.data.length == 0) {
                        this.msg.error(‘未查询到‘);
                    }
                } else {
                    this.msg.error(‘获取数据出错‘);
                }
            });
    }

    constructor() { }

    ngOnInit() {
        this.searchText
            .pipe(debounceTime(300))
            .pipe(distinctUntilChanged())
            .subscribe(string => {
                this.getCode(string);
            });
    }
}

  

【angular7】防抖、截流

原文:https://www.cnblogs.com/dustinsky/p/12167206.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!