0
我試圖設置一個簡單的錯誤通知組件,而在Visual Studio中調試,訂閱,這個似乎是未定義的。RxJS:爲什麼'訂閱'內未定義
public notifications: NotificationMessage[];
constructor(notificationService: NotificationService) {
this.notifications = []; //'this' is defined here
notificationService.rxEmitter.subscribe((notificationMessages: any) => {
this.notifications = notificationMessages; //'this' is undefined here
});
}
編輯:這Screenshot從VS.斷點被未定義有趣的是,'_this',does存在於運行時,儘管我無法引用它,因爲typescript會在編譯時引發未找到的錯誤。
這是不太可能的。箭頭功能不能像那樣工作。實際代碼與您發佈的實際代碼不同,或者您在其他地方發生錯誤。考慮提供http://stackoverflow.com/help/mcve可以複製這個問題,一個普朗克會沒事的。 – estus
您是否嘗試將訂閱呼叫置於ngOnInit方法中?它看起來像是在構造函數實際創建對象之前調用的,所以此時此處未定義。 – Supamiu
@Supamiu不應該這樣,op應該提供一個plnkr,因爲代碼對我來說似乎很好 –