Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何防止浏览器控制台中出现未经授权的错误消息_Angular_Angular5 - Fatal编程技术网

Angular 如何防止浏览器控制台中出现未经授权的错误消息

Angular 如何防止浏览器控制台中出现未经授权的错误消息,angular,angular5,Angular,Angular5,我是新手。现在我在angular中调用了一个页面加载api。现在,若有401错误,那个么它将在浏览器控制台中显示消息。现在我不想显示那个错误,所以我该怎么做呢。因为我试图捕捉服务调用中的错误,但仍然遇到问题。 我的服务代码:- getToken(): Observable<ApiResponse> { return this._http.get(this.hostUrl + '/token').pipe( map((response: Response) =>

我是新手。现在我在angular中调用了一个页面加载api。现在,若有401错误,那个么它将在浏览器控制台中显示消息。现在我不想显示那个错误,所以我该怎么做呢。因为我试图捕捉服务调用中的错误,但仍然遇到问题。 我的服务代码:-

getToken(): Observable<ApiResponse> {
    return this._http.get(this.hostUrl + '/token').pipe(
      map((response: Response) => <ApiResponse>response.json())).catch((e: any) => {
        if (e.status === 401) {
            return Observable.empty();
        }
    });
  }
getToken():可观察{
返回此。_http.get(this.hostUrl+'/token').pipe(
map((response:response)=>response.json()).catch((e:any)=>{
如果(例如,状态===401){
return-Observable.empty();
}
});
}
正如您所看到的,我抛出了Observable.empty(),但仍然在浏览器控制台中以红色标记显示错误。请帮我防止这种情况


谢谢,

您可以通过使用角度HttpClient拦截器来实现这一点。请参阅以下链接

    • 这是不可能的

      http
      错误是特定于浏览器的,不可能从浏览器控制台中删除它们。但是,您可以在subscribe块中处理代码中的错误

      .subscribe接受3个函数回调

      • 成功
      • 错误
      • 完全的
      您可以在错误块中定义错误处理来处理该错误,但它仍将在控制台中记录401错误

      this.xxxService.ttt(someProps).subscribe(
        (response) => {  // this is the response success
      
          // do something in success
        }, (error) => {
          // do something on error
          // here you can check if status code is 401 then do something.
        }, (compoele) => {
          // its like on complete of the observable
      
        }
      );
      

      你可以把它藏起来。如果您使用的是Chrome:DevTools->Settings->Console->Hide network messages。