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>

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

相关推荐
Full Stack Developme几秒前
Java Simple Serial Connector 教程
java·stm32·单片机
xcs194053 分钟前
Java 上位机防空警报系统开发
java·开发语言
落魄江湖行4 分钟前
入门篇二:Nuxt 4路由自动生成:告别手动配置路由的日子
前端·vue.js·typescript·nuxt4
sR916Mecz5 分钟前
Linux 服务器磁盘扩容与目录迁移:rsync + bind mount 实现服务无感迁移(无需修改配置)
java·linux·服务器
UAq6wn76j31 分钟前
.NET源码生成器使用SyntaxTree生成代码及简化语法
java·开发语言·.net
火飞鹰32 分钟前
封装MinIO为starter
java·数据库·spring boot
@atweiwei33 分钟前
Go语言并发编程面试题精讲(上)
java·开发语言·面试·golang·channel
_MyFavorite_38 分钟前
JAVA重点基础、进阶知识及易错点总结(36)Lombok 实战 + 阶段总结
java·开发语言
spencer_tseng1 小时前
AffineTransform cannot be resolved
java