在我的Angular-12項目中,我試圖編輯具有圖像的數據。代碼如下所示:
interface:
export class VehicleResponse {
results!: { vehicle: IVehicle };
}
export interface IVehicle {
id?: number;
registration_number: string;
vehicle_image?: any;
}
service:
getVehicleById(id: number): Observable<VehicleResponse> {
return this.http.get<VehicleResponse>(this.api.baseURL + 'vehicles/fetchbyid/' + id, this.httpOptions);
}
public updateVehicle(id: number, data: any) {
return this.http.post(this.api.baseURL + 'vehicles/update/' + id, data, this.httpOptions1);
}
component
在最終更新之前嘗試檢索和查看數據時,出現以下錯誤:
錯誤類型錯誤:無法讀取位于VehicleEditComponent_Template(vehicle-edit.component.html:245的未定義(讀取'vehicle_image')的屬性
它突出顯示了這一頁:
[src]="vehicledata.vehicle_image ? vehicleImageDirectoryPath+vehicledata.vehicle_image:urlVehicle |'assets/img/no-image.png'”
但是顯示了registration_number。vehicle_image存在。可能是因為圖像為空嗎?
我如何解決這個問題?
Thanks
我認為這里的問題在于
vehicledata
,它是undefined
,你無法讀取它的任何屬性。您可以在我在以下代碼中添加的html中使用
optional chaining operator
:另一種方法是添加
*ngIf
指令,僅在vehicledata
可用時顯示圖像標記。我不確定這是否適用于你,因為我可以看到你已經添加了no-image
url。