0

我有一个实体表。当您单击一行时,您将进入另一个代表实体的页面(查看页面)。该功能使用router.navigate.

当实体被删除时,您无法进入其视图:您会得到 404 尝试打开 url。没有通用的方式调用后端并找出实体是否仍然存在。

所以我需要某种条件导航:如果链接不再有效,我不想导航到它(可能显示一些模式等)。我如何用 Angular 做到这一点?

谢谢!

4

1 回答 1

0

You can use HttpClient to see if your page returns status 200.

For example:

constructor(private http: HttpClient) {
  this.http.get('/').subscribe(res => {
  }, error => {
    if (error.status === 200) console.log(error.status);
  })
}

This will always return error even with 200 status, therefore check for status 200 and then use your navigate. You probably need to unsubscribe once you are done.

于 2019-06-26T16:10:14.493 回答