gpt4 book ai didi

Angular 6 : Cannot set property of undefined

转载 作者:太空狗 更新时间:2023-10-29 18:28:33 25 4
gpt4 key购买 nike

我有一个奇怪的错误,在我的应用程序中阻止我继续前进。我有一个 any[] 类型的变量,它使用我作为表的数据源:

<table mat-table  [dataSource]="EcoSummaryList">

变量被初始化为一个空数组。但是当我什至试图将某些东西分配到数组中时,我收到以下错误:

core.js:12632 ERROR TypeError: Cannot set property 'EcoSummaryList' of undefined at push../src/app/eco-summary/eco-summary.component.ts.EcoSummaryComponent.OnSummaryLoad

相关代码如下:

public EcoSummaryList: any[] = [];

OnSummaryLoad(data: any) {
this.EcoSummaryList = [];
console.log(data);

if (data == undefined || data == null) return;
this.EcoSummaryList = data;

}

数据对象没问题,数据来了没问题,以前也能用。

我试图查看其他帖子以找到答案但没有成功,我还尝试在构造函数中初始化数组,地址为 ngOnInit并从类型 Array<MyClass> 创建 var然后调用new Array<MyClass>();

似乎没有任何效果,有什么建议吗?

更新:即使我注释掉 <table>并刚刚分配了 []进入 var 我仍然遇到同样的错误。

最佳答案

看起来 OnSummaryLoad 未被视为组件方法。不知道为什么。您可能想尝试箭头函数语法。使用它应该可以解决它:

public EcoSummaryList: any[] = [];

OnSummaryLoad = (data: any) => {
this.EcoSummaryList = [];
console.log(data);

if (data == undefined || data == null) return;
this.EcoSummaryList = data;

}

关于 Angular 6 : Cannot set property of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53355159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com