<a class='btn btn-primary' (click)='onSave()' style='width:80px'>
<i class='glyphicon glyphicon-chevron-right'></i> Save
</a>
我上面调用了一个打字稿方法,如下所示
onSave(): void {
// this.SaveShipment();
console.log("before Saving");
let id = this.shipments.findIndex(i=> i.id === parseInt(this._routeParams.get('id').toString()));
this.shipments.splice(id,1);
this._shipmentService.saveShipment((id+1).toString(), this.shipment)
.subscribe(shipment=> this.shipments.push(shipment));
this._router.navigate(['ShipmentDetails',{'id':this._routeParams.get('id').toString()}]);
}
在发生任何事情之前,最后一行被调用。任何人都可以帮忙吗?提前谢谢。
答案 0 :(得分:0)
最后一行不会先被调用。
此代码可能无法按预期工作。这是异步代码,只是安排代码供以后执行:
this._shipmentService.saveShipment((id+1).toString(), this.shipment)
.subscribe(shipment=> this.shipments.push(shipment));
在上面的代码被安排执行后执行此行:
this._router.navigate(['ShipmentDetails',{'id':this._routeParams.get('id').toString()}]);
这可能是你想要的:
this._shipmentService.saveShipment((id+1).toString(), this.shipment)
.subscribe(shipment=> {
this.shipments.push(shipment);
this._router.navigate(['ShipmentDetails',{'id':this._routeParams.get('id').toString()}]);
});
这种方式this._router.navigate(...)
在saveShipment()
的响应到达后执行。