Angular 将FileReader结果保存到数组时出现问题
我有一个方法可以将BLOB格式的图像转换为字符串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
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) {}
}