angular - Angular:过滤器更改后获得结果的正确方法
问题描述
在我的简单应用程序中,我有 FilterComponent、ListComponent 和一项服务(我称之为 GithubService)。
我需要通过 http 到 github 输入 FilterComponent 地址中的搜索词并将结果返回给 ListComponent。
export class GithubService {
baseUrlSearch ="https://api.github.com/search";
initResponse:IResponse = {total_count:0,incomplete_results:false,items:[]}
//subject = new Subject<IResponse>();
subject = new BehaviorSubject<IResponse>(this.initResponse);
constructor(private http: HttpClient) { }
public setSearch(newSearchText:string):void{
if (newSearchText.length<=3) return;
const url=`${this.baseUrlSearch}/users?q=${newSearchText}`;
this.http.get<IResponse>(url).pipe(debounceTime(500)).subscribe(
res=> this.subject.next(res));
}
public getResults():Observable<IResponse>
{
return this.subject.asObservable();
}
// ListComponent class
export class ListComponent implements OnInit {
response$: Observable<IResponse>;
constructor (private service :GithubService)
{}
ngOnInit(): void {
this.response$= this.service.getResults();
}
}
我不知道每次更改 newSearchText 时服务都会订阅。
我应该保持不动还是以某种方式重构,或者每次都取消订阅?
解决方案
推荐阅读
- python - 使用 openCV 和 python 检测图像中的模式和数字
- python-3.x - 减少 Python 中 IF 语句的数量
- android - 使用 play-services-vision 检测人脸并提取该信息
- php - WordPress 在自定义插件中将会话数据从产品页面带到购物车页面?
- php - 根据不包括透明的颜色查找表减少图像颜色
- android - 使用 getActionBar.setTitle() 时应用程序崩溃;同时扩展 FragmentActivity
- google-cloud-platform - Ansible 中的 gsutil 权限
- excel - 在两个工作簿中找到一个匹配的值,然后在满足条件时进行标记
- java - Gui 图形没有出现在 Panel 中
- css - 使用 ::after 伪元素重复 SVG 模式作为边框的剪辑路径