Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何避免组件中的两个请求?_Angular - Fatal编程技术网

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) => {})