Angular Interceptors
Angular Interceptors
Angular Interceptors modify the http requests before they are sent to web server.
Here is the simple LogInterceptor which implements HttpInterceptor and overrides the intercept() method
- @Injectable()
-
export class LogInterceptor implements HttpInterceptor {
-
intercept(
-
request: HttpRequest, next: HttpHandler
-
) : Observable> {
-
console.log(request.url);
-
return next.handle(request);
-
}
-
}
Add interceptor as provider in the module
- import { HTTP_INTERCEPTORS } from '@angular/common/http';
-
import { LogInterceptor } from '...';
-
@NgModule({
-
imports: [
-
HttpClientModule
-
],
-
providers: [
-
{
-
provide: HTTP_INTERCEPTORS,
-
useClass: LogInterceptor
-
}
-
],
-
})
-
export class AppModule { }