Angular 如何避免组件中的两个请求?
我有一个加载数据并呈现数据的组件:Angular 如何避免组件中的两个请求?,angular,Angular,我有一个加载数据并呈现数据的组件: ngOnInit() { this.getPage(); this.eventsService.subjectFilterDistributionReset.subscribe((value) => { if (value) { this.paginationService.reset(); this.loadPage();
ngOnInit() {
this.getPage();
this.eventsService.subjectFilterDistributionReset.subscribe((value) => {
if (value) {
this.paginationService.reset();
this.loadPage();
}
});
this.eventsService.subjectFilterDistribution.pipe(filter((filterUrl) => filterUrl)).subscribe((filterUrl) => {
const page = 1;
this.paginationService.setFilterBy(filterUrl);
this.paginationService.setCurrentPage(page);
this.paginationService.calculateOffsetLimit(page);
this.loadPage();
});
this.eventsService.subjectSortingDistribution.pipe(filter(Boolean)).subscribe((sortedList: ListItem[]) => {
this.paginationService.setSortBy(getSortingString(sortedList));
this.loadPage();
});
}
正如您所看到的,默认情况下,它调用this.getPage()代码>:
它还具有以下功能:
this.eventsService.subjectFilterDistribution.pipe(filter((filterUrl) => filterUrl)).subscribe((filterUrl) => {})
问题是当我路由到此组件并发送消息this.events服务.subjectFilterDistribution
时
它调用this.getPage()代码>,因为它在ngOnIinit中
那么,如何划分默认加载和事件加载
问题是我需要调用方法this.loadPage()代码>仅一次
加载组件时
当事件主题出现时
您可以使用CombineTest方法在tap运算符中移动每个订阅中的逻辑,因为从技术上讲,这些都是副作用
public filterDistributionReset=
this.eventsService.subjectFilterDistributionReset.pipe(
过滤器(res=>!!res),
轻触(=>this.paginationService.reset());
//再加上其他科目
公共组合主题=组合测试(
filterDistributionReset,
过滤器分配,
分类分布);
恩戈尼尼特(){
this.combinedSubjects.subscribe(=>this.loadPage())
}
对不起,伙计,我不明白问题出在哪里,你能更好地描述一下吗?还要在loadDataRequest中添加代码。问题是当加载组件时,它调用this.loadPage()如果有任何事件,它也会调用this.loadPage()代码>。因此,方法this.loadPage()代码>只应调用一次
this.eventsService.subjectFilterDistribution.pipe(filter((filterUrl) => filterUrl)).subscribe((filterUrl) => {})