【发布时间】:2019-07-08 06:56:51
【问题描述】:
为什么我必须将event.body 转换为 JSON 字符串并解析回对象?
this.excelFileService.upload(this.currentFileUpload).subscribe(event => {
if (event.type === HttpEventType.UploadProgress) {
this.progress.percentage = Math.round(100 * event.loaded / event.total);
} else if (event instanceof HttpResponse) {
let excelFile: ExcelFile = JSON.parse(JSON.stringify(event.body));
this.excelFiles.push(excelFile);
}
});
如果我直接将event.body传递给push,则无法编译:
ERROR in src/app/excel-file/excel-file.component.ts(54,30): error TS2345: Argument of type '{}' is not assignable to parameter of type 'ExcelFile'.
Type '{}' is missing the following properties from type 'ExcelFile': filename, path, createdAt
如果我通过event.body[0],它会编译但它是一个空对象{}。
【问题讨论】:
标签: javascript angular typescript httpresponse angular7