[英]Angular TypeError cannot set property 'Data' of undefined
In my code, I am calling this operation in ngOnInit to be able to see previously edited data when the page reopens.在我的代码中,我在 ngOnInit 中调用此操作,以便在页面重新打开时能够看到以前编辑的数据。 StickerData
is a property of IStickerData which is an interface. StickerData
是 IStickerData 的一个属性,它是一个接口。 I keep getting我不断得到
ERROR TypeError: Cannot set property 'StickerData' of undefined
at SafeSubscriber._next (sticker-preview.component.ts:54)
at SafeSubscriber.__tryOrUnsub (Subscriber.js:183)
at SafeSubscriber.next (Subscriber.js:122)
at Subscriber._next (Subscriber.js:72)
at Subscriber.next (Subscriber.js:49)
at FinallySubscriber._next (Subscriber.js:72)
at FinallySubscriber.next (Subscriber.js:49)
at CatchSubscriber._next (Subscriber.js:72)
at CatchSubscriber.next (Subscriber.js:49)
at MapSubscriber._next (map.js:35)
error on subscribe((response: any) => this._stickerData.StickerData = response);
subscribe((response: any) => this._stickerData.StickerData = response);
when I open the page.当我打开页面时。 What am I missing here?我在这里想念什么?
TS: TS:
private _stickerData: IStickerData;
Filter: IFilter;
@Input()
set StickerData(prm: IStickerData) {
if (this._stickerData != prm) {
this._stickerData = prm;
}
}
get StickerData(): IStickerData {
return this._stickerData;
}
ngOnInit() {
this._productionService.getStickerDataList(this.Filter)
.subscribe((response: any) => this._stickerData.StickerData = response);
}
service TS:服务TS:
getStickerDataList(data: IFilter): Observable<IStickerData[]> {
return this._http.post("Production/GetStickerDataList", data);
}
You need to initialize the variable before accessing it's properties.您需要在访问它的属性之前初始化变量。
private _stickerData: IStickerData = Object.create(null);
And it looks like at the moment the variable this.Filter
is also left undefined before sending it to the service.现在看起来变量this.Filter
在发送到服务之前也未定义。
You're doing this._stickerData.StickerData = [...]
but this._stickerData
is not initialized, thus it has the value undefined
.您正在执行this._stickerData.StickerData = [...]
但this._stickerData
未初始化,因此其值为undefined
。
You may want to add a default value with:您可能希望添加一个默认值:
private _stickerData: IStickerData = {};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.