简体   繁体   中英

How can I call an angular service from within a custom rxjs operator?

I'm writing a custom rxjs operator to handle logging HttpClient response errors. My instinct is to put the custom operator in its own file that I'd just include where needed. However, that puts it out of the “Angular world” so how would it get access to my Angular logging service?

Interceptor is one possible solution. Another alternative is create your own http service which i prefer

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)
   } 
}

It just gives you more flexibility, and code are more explicit. With default interceptor you will run into a lot of if else cases because every http call is going through this centralized hub.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM