简体   繁体   English

Angular TypeError 无法设置未定义的属性“数据”

[英]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.

相关问题 正在获取TypeError:无法设置未定义的属性“数据” - Getting TypeError: Cannot set property 'data' of undefined Angular / Typescript无法设置未定义的属性“数据” - Angular / Typescript Cannot set property 'data' of undefined TypeError:无法设置未定义的属性“ - TypeError: Cannot set property '' of undefined TypeError:无法设置未定义的属性“ 0” - TypeError: Cannot set property '0' of undefined angular.js:13550 TypeError:无法设置未定义的属性“people” - angular.js:13550 TypeError: Cannot set property 'people' of undefined Angular:Typescript:未捕获类型错误:无法设置未定义的属性“autoTable” - Angular: Typescript: Uncaught TypeError: Cannot set property 'autoTable' of undefined 3angular.js:13920 TypeError:无法设置未定义的属性&#39;contract&#39; - 3angular.js:13920 TypeError: Cannot set property 'contract' of undefined 错误类型错误:无法设置未定义 ANGULAR 的属性“名称” - Error TypeError: cannot set property “name” of undefined ANGULAR Angular UI-Grid TypeError:无法设置未定义的属性“数据”。 发送来自服务的承诺和未定义的数据 - Angular UI-Grid TypeError: Cannot set property 'data' of undefined. Sending a promise from Service and data coming as undefined 类型错误:无法使用拦截器读取 Angular 中未定义的属性“数据” - TypeError: Cannot read property 'data' of undefined in Angular with Interceptor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM