Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 将FileReader结果保存到数组时出现问题_Angular_Typescript - Fatal编程技术网

Angular 将FileReader结果保存到数组时出现问题

Angular 将FileReader结果保存到数组时出现问题,angular,typescript,Angular,Typescript,我有一个方法可以将BLOB格式的图像转换为字符串 public imagePath; public imagePaths: Book[] = []; imageURL: string; createImageFromBlob(image: Blob): void { var reader = new FileReader(); reader.addEventListener("load", () => { this.imageUR

我有一个方法可以将BLOB格式的图像转换为字符串

public imagePath;
public imagePaths: Book[] = [];
imageURL: string;

createImageFromBlob(image: Blob): void {
      var reader = new FileReader();
      reader.addEventListener("load", () => {
       this.imageURL = reader.result as string;
       this.imagePaths.push(new Book('Book1', this.imageURL));
       console.log(this.imageURL); // OK
       console.log(this.imagePaths[0].image ); // wrong output
      });
      reader.readAsDataURL(image);
}
返回正确的字符串。但是,当我尝试在Book对象中保存imageURL时,由于行

reader.result as string 
给我消息“未定义”

我的书本课:

   console.log('test2' + this.imagePaths[0].image ); 

你的Book类是什么样子的?导出类Book{name:string;image:string;构造函数(name:string,image:string){}你在构造函数中设置了属性吗?因为在那件衣服上看起来不像这样
export class Book{
    name: string;
    image: string;

    constructor(name: string,image: string) {}
}