【问题标题】:Angular 6 subscription invoke multiple timesAngular 6订阅多次调用
【发布时间】:2019-02-12 11:50:40
【问题描述】:

我正在开发一个新的 Angular 6 WebApp,它的组件之间的整个通信都基于主题和订阅。

整个订阅在仪表板组件中实现(根目录下的级别 1)。在仪表板上,组件是从 C# ASP.NET Core 后端使用 SignalR Web 套接字生成的表。

现在我们为该表中的每一行实现了编辑和删除按钮。 编辑按钮打开一个 ngx-bootstrap 模态。模态打开函数在参数中获取单行信息。

在这个“编辑模式”中是一些带有行日期的输入。现在,当我保存更改时,数据将被发送回仪表板组件,该组件使用 .NET Core API 为行调用更新方法。

在编辑模式和仪表板之间的数据流期间,我的订阅调用了 4 次。

我已尝试调试此问题,但没有找到任何线索...

我的通信服务:

export class CommunicationService {

  private subject = new Subject<any>();

  constructor() { }

  sendData(data: any) {
    this.subject.next({ data });
  }

  removeData() {
    this.subject.next();
  }

  getData(): Observable<any> {
    return this.subject.asObservable();
  }

}

编辑模式:

sendOrderToDashboard(): void {
    // this.order is a data of singel row. Its a table with different user orders as a row.
    this.communicationService.sendData(this.order);
    this.modalRef.hide();
  }

仪表板组件

public ngOnInit(): void {
    this.communicationService.getData().subscribe(orderToUpdate => {
      if (orderToUpdate) {
        this.updateOrder(orderToUpdate.data);
        console.log(orderToUpdate.data);
        // this log invokes 4x times
      }
    });
  }

updateOrder(order): void {
    this.orderService.updateOrder(order).subscribe(next => {
      console.log('updated successfully');
      // This log is never executed even when the update is successful
    }, error => {
      console.log('error while updating order');
    });
  }

在 OrderService 中更新订单(Angular 和 SignalR(后端)之间的桥梁)

  public updateOrder(order: Order): Observable<Order> {
    if (!this.orderSubjects[order.id]) {
      this.orderSubjects[order.id] = new Subject<Order>();
    }

    this.hubService.invoke(ServerMethod.UpdateOrder, order);

    return this.orderSubjects[order.id].asObservable();
  }

有人知道为什么我的成功日志从未执行过并且我的调用次数是 4 次吗?

【问题讨论】:

    标签: angular rxjs observable angular2-observables subject-observer


    【解决方案1】:

    在您的仪表板组件中,我看不到导出类表达式,请检查您是否实现了OnDestroy。

    你的班级应该是这样的

    import { Component, OnInit, OnDestroy } from '@angular/core';
    export class DashboardComponent implements OnInit, OnDestroy {
        ngOnDestroy(){
           if(this.this.subscription){
              this.subscription.unsubscribe();
           }
        }
        // the rest of your code
    }
    

    如果你不实现它,你只是在创建一个名为 ngOnDestroy 的函数,除非你手动调用它,否则它不会被执行。

    【讨论】:

      【解决方案2】:

      每次在 DashBoardComponent 中使用 ngOnDestroy 方法时都需要取消订阅。

      仪表板组件

      private subscription:
      
      updateOrder(order): void {
          if(this.this.subscription){
            this.subscription.unsubscribe();
          }
          this.subscription = this.orderService.updateOrder(order).subscribe(next => {
            console.log('updated successfully');
            // This log is never executed even when the update is successful
          }, error => {
            console.log('error while updating order');
          });
        }
      
      ngOnDestroy(){
         if(this.this.subscription){
            this.subscription.unsubscribe();
         }
      }
      

      【讨论】:

      • 我已经尝试过了,但我的退订从未执行。
      • 更新了代码部分。请试试这个。
      【解决方案3】:

      出于调试目的,我在仪表板组件中添加了更多日志。我在ngOnDestroy() 的订阅中添加了unsubscribe(),但从未执行过此方法。

      updateOrder(order): void {
          console.log('update method is called')
          this.subscription = this.orderService.updateOrder(order).subscribe(next => {
            console.log('updated successfully');
          }, error => {
            console.log('error while updating order');
          });
        }
      
        public ngOnInit(): void {
          console.log('data is available now');
          this.communicationService.getData().subscribe(orderToUpdate => {
            if (orderToUpdate) {
              this.updateOrder(orderToUpdate.data);
              console.log('update init');
              console.log(orderToUpdate.data);
            }
          });
        }
      
        public ngOnDestroy(): void {
          if (this.subscription) {
            this.subscription.unsubscribe();
            console.log('successfuly unsubscribe');
          }
        }
      

      似乎整个组件都初始化了 2 次?并且取消订阅永远不会执行。我在下面添加了一个截图,console.log

      Console Logs

      【讨论】:

      • 您的 CommunicationService 是否有可能多次实例化?能不能在构造函数里放个日志?
      猜你喜欢
      • 2019-04-21
      • 2018-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-21
      • 2018-04-06
      • 2019-03-25
      • 2021-06-09
      相关资源
      最近更新 更多