【angular7】防抖、节流


...
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);
            });
    }
}

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM