我定义并初始化了(两次)我的数组,但我在运行时收到“错误类型错误:无法读取未定义的属性‘长度’”。
我已经尝试检查数组是否已定义,如您在我的代码中看到的那样,但 Angular 仍然只是在欺骗我。 我已经尝试了所有解决方案,例如将 sintax 更改为“filesToUpload && filesToUpload?.length > 0”或任何可能的组合,但是在我看来,Angular 无法正常工作。
component.html 文件
<ng-container *ngIf="undefined !== filesToUpload && filesToUpload.length > 0">
<button (click)='uploadImages()'
class="btn btn-success"
type="button">
<i class="fas fa-upload"></i>Upload
</button>
<button (click)='cancelUpdate()'
class="btn btn-danger"
type="button">Cancel</button>
</ng-container>
这是我的 component.ts 文件
export class ImageGalleryUploadComponent implements OnInit
{
filesToUpload: File[] = [];
ngOnInit() {
this.filesToUpload = []
}
}
这是一个 Angular 错误吗?或者与组件生命周期有关?
最佳答案
尝试一下,在某个地方将 undefined
分配给您的 Array 对象。
<ng-container *ngIf="filesToUpload && filesToUpload != 'undefined' && filesToUpload.length > 0">
虽然这不是一个好的做法,但只需尝试一次,如果确认,则在您的代码中进行交叉检查。
关于javascript - 为什么我在定义的数组上收到 "ERROR TypeError: Cannot read property ' length' of undefined"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54192536/