angular - 向 Angular App 添加加载功能
问题描述
每当我在应用程序的搜索栏上键入内容时,我都会尝试实现加载。但似乎只有第一个“tap”函数被调用并将加载值设置为 true。我最终没有将 true 设置为 false。关于我应该如何做到这一点的任何帮助/建议?
沿用了这种做法。
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);
}
}
解决方案
尝试做
this.courses$ = this.searchString$.pipe(
debounceTime(300),
distinctUntilChanged(),
,do( () => this.loading = true)
switchMap((term: string) => this.cs.getCoursess(term)),
,do( () => this.loading = false )
);
推荐阅读
- php - 伪造 JSON 数据的最佳实践 Laravel 5.2
- javascript - 如何使字符串成为javascript代码的一部分?
- javascript - 如何通过 rxjs 运算符操作从请求中获取的对象?
- linux - /sys/dev/char 如何在 Linux 中填充?
- c# - 如何检查特定作业是否在石英调度程序c#中运行
- knockout.js - 将一个模型数据访问到另一个模型的函数中的淘汰赛
- java - 执行失败
- python - Flask Marshmallow - 模式返回属性错误?
- java - 我的数组的第一个位置没有被修改
- php - **SOAP-错误:** 编码:找不到编码