मेरे पास एक घटक है जो एक सेवा को RESTful समापन बिंदु से डेटा लाने के लिए कहता है। इस सेवा को डेटा कहा जाने के बाद निष्पादित करने के लिए कॉलबैक फ़ंक्शन देने की आवश्यकता है।
समस्या यह है कि जब मैं किसी घटक के चर में मौजूदा डेटा को डेटा को जोड़ने के लिए कॉलबैक फ़ंक्शन का उपयोग करने का प्रयास करता हूं, तो मुझे ए मिलता है EXCEPTION: TypeError: Cannot read property 'messages' of undefined। thisअपरिभाषित क्यों है ?
टाइपस्क्रिप्ट संस्करण: संस्करण 1.8.10
नियंत्रक कोड:
import {Component} from '@angular/core'
import {ApiService} from '...'
@Component({
...
})
export class MainComponent {
private messages: Array<any>;
constructor(private apiService: ApiService){}
getMessages(){
this.apiService.getMessages(gotMessages);
}
gotMessages(messagesFromApi){
messagesFromApi.forEach((m) => {
this.messages.push(m) // EXCEPTION: TypeError: Cannot read property 'messages' of undefined
})
}
}
tsc -v)