Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

255
Views
¿Cómo puedo obtener datos del servicio en el interceptor angular?

Quiero agregar mi token de autenticación en el encabezado de solicitud en mi auth.interceptor.ts y mantengo el valor del token de autenticación en mi auth.service.ts . Aquí está auth.interceptor.ts

 @Injectable() export class AuthInterceptor implements HttpInterceptor { constructor(private router: Router, private authService: AuthService) { } intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { console.log('intercept', request); const authReq = request.clone({headers: request.headers.set(this.authService.getAuthenticationKey(), this.authService.getAuthenticationToken())}); return next.handle(request); } }

y aquí hay parte de auth.service.ts

 ... public getAuthenticationToken(): string { return this.authenticationToken; } public getAuthenticationKey(): string { return this.authenticationKey; } ...

Y aquí está mi app.module.ts

 providers: [ { provide: HTTP_INTERCEPTORS, useFactory: (router: Router, authService: AuthService) => { return new AuthInterceptor(router, authService); }, multi: true, deps: [Router] } ],

pero me sale este error

 TypeError: Cannot read properties of undefined (reading 'getAuthenticationKey')

¿Cuál es el problema?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Agregue AuthService a las dependencias ( deps ). Como la función de fábrica tiene acceso a AuthService , debe inyectarlo en el proveedor de fábrica.

 providers: [ { provide: HTTP_INTERCEPTORS, useFactory: (router: Router, authService: AuthService) => { return new AuthInterceptor(router, authService); }, multi: true, deps: [Router, AuthService] } ],

Referencias

Uso de proveedores de fábrica

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!