【发布时间】:2019-01-27 23:51:42
【问题描述】:
每当我在应用程序的搜索栏上键入内容时,我都会尝试实现加载。但似乎只有第一个“tap”函数被调用并将加载值设置为 true。我最终没有将 true 设置为 false。关于我应该如何做到这一点的任何帮助/建议?
关注了这个approach。
import { Component, OnInit } from '@angular/core';
import { CourseService } from './course-service';
import { Observable, BehaviorSubject, combineLatest, Subject, from } from 'rxjs';
import { debounceTime, distinctUntilChanged, switchMap, tap, finalize } from 'rxjs/operators';
@Component({
selector: 'explore',
templateUrl: './explore.component.html'
})
export class ExploreComponent implements OnInit {
loading: boolean = false;
private searchString$ = new Subject<string>();
courses$: Observable<any[]>;
constructor(private ar: ActivatedRoute, public cs: CourseService) {
}
ngOnInit() {
this.courses$ = this.searchString$.pipe(
debounceTime(300),
distinctUntilChanged(),
tap(() => this.loading = true),
switchMap((term: string) => this.cs.getCoursess(term)),
tap(() => this.loading = false)
);
}
search(text: string) {
this.searchVal = text;
this.searchString$.next(text);
}
}
【问题讨论】:
-
添加 catchError 以确保在发生错误时您的加载变为错误: pipe(...,catchError(()=>this.loading=false)
标签: angular typescript