【发布时间】:2020-07-15 06:34:24
【问题描述】:
我有一个从后端 API 检索到所有 src 的图像列表:
<div class="repoContent">
<div *ngFor="let item of repo.value" class="repoItem">
<img [src]="fetchImage(item.name)" (click)="selectAsset(fetchImage(item.name))" alt="">
</div>
</div>
用户看到图片后,可以点击图片将一张图片放到 ngx-image-cropper 进行编辑。在这里,我再次使用 url 作为输入。 (可能是 blob base64)
问题是:
每次我单击图像时,都会向我的后端触发另一个 http 请求以获取图像,而我假设这些请求是在本地缓存的。正如我所看到的这些图像,我认为数据存储在客户端计算机上的某个位置,然后我可以在不使用 url 的情况下将 blob 数据传递给 ngx-image-cropper 以避免再次请求后端。我怎么能这样做?
功能更新:
selectAsset(url){
this.imageSelected=true;
this.imageFile=null;
this.imageURL=url;
}
fetchImage(name:String):String{
return GlobalParameterService.apiAssetGet+"?name="+name;
}
selectAsset 重置裁剪器并放入 url。
fetchImage 构建目标 url
【问题讨论】:
-
你能显示函数
fetchImage()和selectAsset()吗? -
@MichaelD 是的,我添加了它们
-
不要将
src绑定到函数,从生命周期挂钩调用函数并将结果分配给组件中的属性。