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

634
Views
Usando 'esto' dentro de una devolución de llamada de observador

Tengo una aplicación Angular que acabo de actualizar de v8 a v12.

La forma en que manejé las respuestas a los observables ahora está obsoleta y, aunque cambié mi código, 'esto' ya no funciona dentro de la devolución de llamada y me pregunto cuál es el mejor enfoque para resolver esto.

Así es como solía manejar los observables;

 this._trialService.currentTrial().subscribe(trial => { this.trial = trial; this.loadVisitDetails() }, error => { this._alertService.showWarningAlert(error.error.title); });

Lo he cambiado a esto;

 this._trialService.currentTrial().subscribe({ next(trial) { this.trial = trial; this.loadVisitDetails() }, error(error) { this._alertService.showWarningAlert(error.error.title); } });

Debido a que el código ya no usa funciones de flecha, this ya no se refiere a la clase principal, por lo que ya no puedo acceder a las propiedades y métodos en esa clase principal.

¿Hay alguna manera de evitar esto, o tendré que crear una variable fuera de la devolución de llamada que se refiera a this ?

 const self = this; this._trialService.currentTrial().subscribe({ next(trial) { self.trial = trial; self.loadVisitDetails() }, error(error) { self._alertService.showWarningAlert(error.error.title); } });

Eso parece un poco desordenado.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Todavía puede usar funciones de flecha para los controladores:

 this._trialService.currentTrial().subscribe({ next: (trial) => { this.trial = trial; this.loadVisitDetails() }, error: (error) => { this._alertService.showWarningAlert(error.error.title); } });
about 4 years ago · Juan Pablo Isaza 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!