簡體   English   中英

Angular中使用routerLink時如何處理異常

[英]How to handle exceptions when using routerLink in Angular

我有一個帶有routerLink的組件,並且路由模塊有一個路由。 ngOnInit 調用服務

this.myService.create(aId).subscribe(
data => { this.ourdata = data;
this.dataSource = new MatTableDataSource<test>(data.some);
},
err => console.error(err),
() => console.log('Done '+ JSON.stringify(this.ourdata))
);

和 html 顯示微調器,直到收到數據

<div *ngif="!ourdata">
<mat-spinner></mat-spinner>
</div>
<div *ngif="ourdata" class="centering-div">
...
</div>

如果出現異常,如何顯示對話框並停止微調器? 我正在尋找 html 模板的變化

您可以使用訂閱的err回調。

error: boolean;
.....
this.myService.create(aId).subscribe(
data => { 
   this.ourdata = data;
   this.error = false;
   this.dataSource = new MatTableDataSource<test>(data.some);
},
err => { 
   console.error(err); 
   this.error = true;
   // call method that shows the dialog
   // or show the dialog in the HTML based on the value of error being true
}
() => console.log('Done '+ JSON.stringify(this.ourdata))
);
<div *ngif="!ourdata && !error">
<mat-spinner></mat-spinner>
</div>
<div *ngif="ourdata" class="centering-div">
...
</div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM