Tengo una clase en js: cómo puedo modificar el cuerpo en una función anidada en la línea n. ° 4. Estoy recibiendo una advertencia del IDE:
Potentially invalid reference access to a class field via 'this.' of a nested function
¿Hay alguna solución para escribir esto correctamente? ¿O puedo usar this
función anidada dentro?
fetchStickyAppointment = flow(function* () { fetch(/api/) .then(response => response.json()) .then(data => { if (+data?.id > 0) { this.stickyAppointment = this.currentAppointment = data; } else { this.stickyAppointment = data; this.currentAppointment = this.findCurrentAppointment(); } }); })
Puede asignar this
a una variable fuera del alcance de la función y luego usar eso.
let self = this; fetchStickyAppointment = flow(function* () { fetch(/api/) .then(response => response.json()) .then(data => { if (+data?.id > 0) { self.stickyAppointment = self.currentAppointment = data; } else { self.stickyAppointment = data; self.currentAppointment = self.findCurrentAppointment(); } }); })