vue3 渲染一个后端返回的图片字段渲染、table表格内放置图片

一、后端直接返回图片url

当图片字段接口直接返回的是图片url,可以直接放到img标签上

<img v-if="thumbLoader" class="r-image-loader-thumb" :src="resUrl" />

二、当图片字段接口直接返回的是图片Id

那么就需要去拼一下图片链接

调用下接口吧返回的id传过去拼接即可

复制代码
<img v-if="thumbLoader" class="r-image-loader-thumb" :src="imgSrc" />

 

const imgSrc = computed(() => {
    
        return `/bnc_rest/market/file/${resUrl};
     
});

三、接口直接返回的是图片文件

需要自行拼接

复制代码
------------------------结构部分
<img v-if="thumbLoader" class="r-image-loader-thumb" :src="logo" />

------------------------js部分
import { getEnterpriseLogo } from "@/api/index.js";

const logo = computed(() => {
    let url = "";
    if (props.detail && props.detail.enterpriseVO && 
    props.detail.enterpriseVO.enterpriseId) {
   //在api.js文件里写个方法调用 或者直接拼
      //1.调用的方法
      url = getEnterpriseLogo(props.detail.enterpriseVO.enterpriseId);
      //2.直接在当前页面拼接
      // url = `/bnc_rest/org/orgunits/${props.detail.enterpriseVO.enterpriseId}/logo`;
    }
    return url;
});

api/index.js文件

复制代码
/**
 * 获取企业logo
 */
export const getEnterpriseLogo = id => {
    return `/bnc_rest/org/orgunits/${id}/logo`;
};

四、在table表格里去放置图片字段

用的ui组件库为arco-deiagn vue 组件库

复制代码
 <a-table
                column-resizable
                :bordered="{ headerCell: true, wrapper: false }"
                :data="rtableTableData"
                :scrollbar="false"
                :pagination="false"
                :row-selection="rowSelection"
                @row-click="rowClick"
                v-model:selected-keys="selectedKeys"
                :row-key="rowKey"
                v-bind="{ ...OtherProps }"
                :scroll="scrollConfig"
                @select="rowSelect"
                @select-all="rowAllSelect"
                :span-method="spanMethod"
                ref="tableRef"
            >
                <template #summary-cell="{ column, record }">
                    <div>{{ record[column.dataIndex] }}</div>
                </template>
                <template #columns>
                    <a-table-column align="left" title="序号" v-if="showSeq" :width="showSeqWidth" class="first">
                        <template #cell="{ rowIndex }">{{ currentPageStart + rowIndex }}</template>
                    </a-table-column>
                    <a-table-column
                        v-for="(c, i) in columns"
                        :width="c.width ? c.width : i + 1 < columns.length ? 120 : undefined"
                        :align="c.align ? c.align : 'left'"
                        :title="c.title"
                        :data-index="c.dataIndex"
                        :fixed="c.fixed ? c.fixed : ''"
                        :key="i"
                    >
                        
                        <template #cell="{ rowIndex, record, column }">
                            <!-- 自定义单元格内容 -->
                            <slot
                                :name="c.slotName ? c.slotName : c.dataIndex"
                                :rowIndex="rowIndex"
                                :record="record"
                                :cell="record[column.dataIndex]"
                            >
                                <!-- 图片 -->
                                <template v-if="c.type && c.type == 'img'">
                                    <RImage
                                        v-if="record[column.dataIndex]"
                                        fit="cover"
                                        height="32"
                                        width="100%"
                                        :src="record[column.dataIndex]"
                                        show-loader
                                        class="r-table-content-img"
                                    ></RImage>
                                    <span v-else>-</span>
                                </template>
                                <!-- 附件 -->
                                <template v-else-if="c.type && c.type == 'file'">
                                    <RLink v-model="record[column.dataIndex]" v-if="record[column.dataIndex]"></RLink>
                                    <span v-else>-</span>
                                </template>
                                <div v-else>{{ formatterCell(record, c) }}</div>
                            </slot>
                        </template>
                    </a-table-column>
  </a-table>

此处为表格内单元格的图片放置,对其设置一个表格插槽即可

相关推荐
崔庆才丨静觅1 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
曹牧2 小时前
Spring Boot:如何测试Java Controller中的POST请求?
java·开发语言
passerby60612 小时前
完成前端时间处理的另一块版图
前端·github·web components
掘了2 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅2 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅3 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
爬山算法3 小时前
Hibernate(90)如何在故障注入测试中使用Hibernate?
java·后端·hibernate
kfyty7253 小时前
集成 spring-ai 2.x 实践中遇到的一些问题及解决方案
java·人工智能·spring-ai
猫头虎3 小时前
如何排查并解决项目启动时报错Error encountered while processing: java.io.IOException: closed 的问题
java·开发语言·jvm·spring boot·python·开源·maven
李少兄3 小时前
在 IntelliJ IDEA 中修改 Git 远程仓库地址
java·git·intellij-idea