No me funciona la clase.
Saludos, estoy trabajando Angular 18 y me ha ocurrido lo siguiente:
- HttpClientModule está deprecado, por lo tanto tuve que proveer provideHttpClient().
- La clase que ud propuso no me funcionó, no sé si fue por no haber importado HttpClientModule. El caso fue que tuve que crear la constante como lo propone el ng g interceptor, además de insertar esta funcion dentro del provideHttpClient (provideHttpClient(withInterceptors([interceptorInterceptor])));
app.module.ts
{ NgModule } from [@ [@ [@ [@ [@'@angular](https://ed.team/u/'@angular)](https://ed.team/u/'@angular)](https://ed.team/u/'@angular)](https://ed.team/u/'@angular)](https://ed.team/u/'@angular)/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { provideHttpClient, withInterceptors } from '@angular/common/http';
import { interceptorInterceptor } from './interceptor/interceptor.interceptor';
[@
<a href="/u/NgModule" data-community-mention="USER" data-community-mention-slug="NgModule">@NgModule</a>](https://ed.team/u/
<a href="/u/NgModule" data-community-mention="USER" data-community-mention-slug="NgModule">@NgModule</a>)({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
],
providers: [provideHttpClient(withInterceptors([interceptorInterceptor]))],
bootstrap: [AppComponent]
})
export class AppModule { }
interceptor.Interceptor.ts
{ HttpEvent, HttpHandler, HttpInterceptor, HttpInterceptorFn, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
export const interceptorInterceptor: HttpInterceptorFn = (req, next) => {
console.log("boooo");
const requestModi = req.clone({
setHeaders: {
'X-App-Version': '1.0.0',
'cosa': '1234123412'
}
});
return next(requestModi);
};
// [@Injectable](https://ed.team/u/Injectable)()
// export class Interceptor implements HttpInterceptor {
// intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// console.log("sdfasdf");
// const requestModi = req.clone({
// setHeaders: {
// 'X-App-Version': '1.0.0',
// 'cosa': '1234123412'
// }
// });
// return next.handle(requestModi);
// }
// }