Angular2行执行问题

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

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

贡特·佐赫鲍尔(GünterZöchbauer)

最后一行不会首先被调用。

此代码可能无法按您预期的那样工作。这是异步代码,只是计划代码以供以后执行:

  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()到达后执行此方法

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章