vue3中在elementplus多行表格中渲染多图数组中首图的显示问题,无法正常显示图片,作用域插槽写法问题

背景

vue3 中使用elementplus的el-table组件 的时候,此时我在vue生命周期中挂载时获取了到服务器了数据,这个数据是一个多个对象的数组 ,各个对象又包括了图片数组,此时我想在表格上的每一行渲染图片的首图 ,也就是下标为0的图片,但是出现图片无法正常显示

vue中写法部分代码,tableData是已知的数组:

html 复制代码
<template>
  <el-table :data="tableData" style="width: 100%">
    <el-table-column fixed prop="pm" label="品名" width="150" />
    <el-table-column prop="bt" label="标题" width="120" />
    <el-table-column prop="jg" label="价格" width="120" />
    <el-table-column prop="td" label="特点" width="120" />
    <el-table-column prop="jhl" label="净含量" width="120" />
    <el-table-column prop="bz" label="包装" width="120" />
    <el-table-column prop="cd" label="产地" width="120" />
    <el-table-column prop="bzrq" label="保质日期" width="120" />
    <el-table-column prop="syff" label="食用方法" width="120" />
    <el-table-column prop="plb" label="配料表" width="300" />
    <el-table-column prop="zmwz" label="致敏物质" width="120" />
    <el-table-column prop="zctj" label="贮存条件" width="120" />
    <el-table-column prop="wxts" label="温馨提示" width="120" />
    <el-table-column prop="tp" fixed="right" label="首图" width="120">
      <template slot-scope="scope">
        <el-image v-if="scope && scope.row" style="width: 100px; height: 100px" :src="scope.row.tp[0]"  />
        <span v-else>无图片</span>
      </template>
    </el-table-column>
    <el-table-column fixed="right" label="操作" width="120">
      <el-button link type="primary" size="small">修改</el-button>
      <el-button link type="primary" size="small" @click="handleClick">删除</el-button>
    </el-table-column>
  </el-table>
</template>

出现的问题

首图无法显示,出现这个scope问题。Property "scope" was accessed during render but is not defined on instance.

解决方案

使用v-slot替代slot-scope:在Vue 2中,你可以使用slot-scope来获取作用域插槽的数据。但在Vue 3中,slot-scope已被废弃,取而代之的是使用v-slot指令。你可以尝试将slot-scope="scope"替换为v-slot="scope"。

部分改正后代码:

html 复制代码
<template v-slot="scope">
  <el-image v-if="scope && scope.row" style="width: 100px; height: 100px" :src="scope.row.tp[0]"  />
  <span v-else>无图片</span>
</template>

然后就好啦~

如果这篇对你有帮助的话~点个赞赞再走吧~~~~~~~

相关推荐
光影少年2 小时前
angular生态及学习路线
前端·学习·angular.js
記億揺晃着的那天4 小时前
Vue + Element UI 表格自适应高度如何做?
javascript·vue.js·ui
无尽夏_4 小时前
HTML5(前端基础)
前端·html·html5
Jagger_4 小时前
敏捷开发流程-精简版
前端·后端
FIN66685 小时前
昂瑞微冲刺科创板:创新驱动,引领射频芯片国产化新征程
前端·安全·前端框架·信息与通信·芯片
GISer_Jing5 小时前
ByteDance——jy真题
前端·javascript·面试
睡美人的小仙女1275 小时前
浏览器为何屏蔽本地文件路径?
前端
真的想不出名儿5 小时前
Vue 中 props 传递数据的坑
前端·javascript·vue.js
FIN66685 小时前
昂瑞微:深耕射频“芯”赛道以硬核实力冲刺科创板大门
前端·人工智能·科技·前端框架·信息与通信·智能
阳光阴郁大boy5 小时前
星座运势网站技术解析:从零打造现代化Web应用
前端·javascript