Javascript 角度+;使用FileReader错误处理文件列表

Javascript 角度+;使用FileReader错误处理文件列表,javascript,html,angular,Javascript,Html,Angular,有人能解释一下为什么this.imagesResults.push({src:reader.result})引发此错误:无法读取未定义的属性“imagesResults” 我试图将每个文件从文件列表转换为Base64,并将结果放入数组中,我不明白为什么它说ImageResults未定义,我尝试了所有可能的方法,甚至尝试设置超时以确保它已定义,但我无法理解 import {AfterViewInit, Component, Input, OnInit} from '@angular/core';

有人能解释一下为什么
this.imagesResults.push({src:reader.result})引发此错误:
无法读取未定义的属性“imagesResults”

我试图将每个文件从文件列表转换为Base64,并将结果放入数组中,我不明白为什么它说ImageResults未定义,我尝试了所有可能的方法,甚至尝试设置超时以确保它已定义,但我无法理解

import {AfterViewInit, Component, Input, OnInit} from '@angular/core';
import {ModalController} from "@ionic/angular";

@Component({
  selector: 'app-file-handler',
  templateUrl: './file-handler.page.html',
  styleUrls: ['./file-handler.page.scss'],
})
export class FileHandlerPage implements AfterViewInit {

  @Input() f: FileList;
  imagesResults = [];

  constructor(private modalController: ModalController) { }

  ngAfterViewInit(): void {
    [].forEach.call(this.f, this.readAndPreview);
    }

  readAndPreview(file) {

    const imageResults = [];
    // Make sure `file.name` matches our extensions criteria
    if ( /\.(jpe?g|png|gif)$/i.test(file.name) ) {
      const reader = new FileReader();

      reader.addEventListener('load',  () => {
        this.imagesResults.push({src: reader.result});
      })

      reader.readAsDataURL(file);
    }

  }

  dismiss() {
    this.modalController.dismiss();
  }

}

这不是简单的JavaScript。这是一个有棱角的字体。可能
call()
会在运行时创建一个闭包并隔离组件

试着这样做:

ngAfterViewInit(): void {
    this.f.forEach( file => { 
        this.readAndPreview(file);
    }
}

如果只想将文件预览为图像,请改用URL.createObjectURL。