【发布时间】: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