遇到的问题是,table列表中有图片,但后端只给了id,而且图片预览有权限认证,所以直接把图片路劲放入src行不通,只能异步去拿,首先我想到的办法是,在插槽中去调用图片预览的方法:就像这种
<--! htm -->
<div class="custom-table-column-icon">
<span v-if="scope.headImgUrl" ><img height="48px" width="48px" :src="scope.imgUrl"/>{{scope.imgUrl?'':viewImg(scope)}}</span>
<span class="author" v-else>{{ scope.employeeName.substring(scope.employeeName.length - 2) }}</span>
</div>
<--! js -->
viewImg(scope) {
API.userManage.previewFile(scope.headImgUrl).then(res => {
let blob = res.data;
if (res.data.data) {
blob = res.data.data
}
let reader = new FileReader();
reader.readAsDataURL(blob); // 转换为base64
reader.onload = function () {
// resolve(reader.result)
scope['imgUrl'] =reader.result
}
这样确实可以把图片下载保存到列表中,但是dom却不能刷新,试过this.$forceUpdate
这是异步的,会死循环。
所以后面我采用了一个笨方法:每次图片下载完更新一次dom:
最后的效果: