I just upgraded my Angular v4.2.5
app to Angular v4.3.6
primarily to use Interceptors
. It is quite an awesome feature and provides a clean way to intercept HTTP calls.
However, it seems I cannot scope interceptors at modules level. For example, I have an AppModule
, and two more modules AModule
and BModule
. Both AModule
and BModule
are getting included in AppModule
.
Now, is there a way in Angular 4 where I can scope interceptors at module level such that an interceptor I use for HTTP requests in AModule
shouldn't be used with HTTP requests in BModule
. Presently, interceptors are getting shared across all HTTP calls which was not I expected. I know providers
from all modules get merged into the parent module, but is there any way to restrict such thing?
Any help is highly appreciated.
Interceptors are one of the built-in tools for specifically handling HTTP requests at a global application level. Often we want to enforce or apply behavior when receiving or sending HTTP requests within our application. Interceptors are a unique type of Angular Service that we can implement.
We can use Interceptors to intercept the incoming and outgoing HTTP requests and manipulate them using Angular HttpClient. One of the most use cases for adding an interceptor to our Angular application is adding the token to the header of outgoing HTTP requests. Let’s implement an interceptor in our Angular application!
Interceptors allow us to intercept incoming or outgoing HTTP requests using the HttpClient. They can handle both HttpRequest as well as HttpResponse. By intercepting the Http request, we can modify or change the value of the request, and by intercepting the response, we can do some predefined actions on a particular status code or message.
From the Angular documentation: Although interceptors can modify requests and responses, the HttpRequest and HttpResponse instance properties are readonly, rendering them largely immutable.
Add a header or flag to the request from the different services? And then use this to delineate where it is from.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With