Angular2 执行线问题

Angular2 line of execution issue

<a class='btn btn-primary' (click)='onSave()' style='width:80px'>
<i class='glyphicon glyphicon-chevron-right'></i> Save
</a>  

我上面调用了一个 typescript 方法,如下所示

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()}]);
} 

最后一行在任何事情发生之前首先被调用。任何人都可以帮忙吗?提前致谢。

最后一行没有首先被调用。

此代码可能无法按您预期的方式运行。这是异步代码,只是安排稍后执行的代码:

  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()的响应到达后执行。