如何在angular 8中使用snackbar添加错误处理,而不使用集中式错误处理

如何在angular 8中使用snackbar添加错误处理,而不使用集中式错误处理,angular,error-handling,snackbar,Angular,Error Handling,Snackbar,我在client.add.component.ts中有此代码,希望对重复条目执行错误处理。我得到503错误控制台目前 this.clientsSvc.createClient(client).subscribe(x => this.router.navigate(['../'], {relativeTo: this.route})); 如何在上述代码中添加以下行 this.snackBar.open(`Duplicate Client Name`, null, {duration: 30

我在client.add.component.ts中有此代码,希望对重复条目执行错误处理。我得到503错误控制台目前

this.clientsSvc.createClient(client).subscribe(x => this.router.navigate(['../'], {relativeTo: this.route}));
如何在上述代码中添加以下行

this.snackBar.open(`Duplicate Client Name`, null, {duration: 3000});

我相信您只想在
createClient的当前请求失败时显示/处理错误

在这种情况下,请检查错误回调中的状态代码

this.clientsSvc.createClient(client)
.subscribe(
   x => this.router.navigate(['../'], {relativeTo: this.route}),
   error => {
     if(error.status === 503) {
       this.snackBar.open(`Duplicate Client Name`, null, {duration: 3000});
     }

   }
);

我在client.service.ts文件中有以下代码。createClient(post:Partial){返回this.http.post(this.api_url,post).pipe(点击(=>this._updateClients.next(null));}如何更改此设置?我认为您在错误回调中总是有完整的响应,不是吗?我认为“observe”参数只适用于“success”回调…@smiti您不必更改
client.service.ts
change
this.clientsSvc.createClient(client).subscribe(x=>this.router.navigate(['..'],{relatieto:this.route}))与我在answer@Random我的错。你说得对,更新了答案。谢谢你的提醒:)