![](/img/trans.png)
[英]How can I handle a 401 Unauthorized response differently from other errors in an Angular 8 service using RxJS?
[英]How can I call an angular service from within a custom rxjs operator?
我正在編寫一個自定義rxjs運算符來處理記錄HttpClient響應錯誤。 我的直覺是將自定義運算符放在自己的文件中,該文件僅在需要時包括在內。 但是,這使它脫離了“ Angular世界”,因此它將如何訪問我的Angular日志記錄服務?
攔截器是一種可能的解決方案。 另一種選擇是創建自己的http服務,我更喜歡
class Http{
constructor(private http:HttpClient){}
logging = (res)=>tap(res=>log(res))
preIntercept(options)=>options
post(options){
options=preIntercept(options)
return http.post(options).pipe(logging)
}
}
它只是為您提供了更大的靈活性,並且代碼也更加明確。 使用默認攔截器,您會遇到很多if else
情況,因為每個http調用都通過此集中式中心。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.