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>

然后就好啦~

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

相关推荐
汪子熙1 小时前
CSS Style position: absolute 的含义
前端·css
算法与编程之美1 小时前
通过两个类计算一个长方形的周长和面积
java·开发语言·javascript·jvm·servlet
晴天蜗牛不一般1 小时前
隆重的给大家介绍一下 <BaseEcharts/>
前端·npm·echarts
ceek@1 小时前
HTML增加复制模块(使用户快速复制内容到剪贴板)
前端·javascript·html
小于负无穷1 小时前
前端面试题(十)
前端
Tim20231 小时前
202409使用eslint与prettier的实战
前端
黄毛火烧雪下2 小时前
前端注释规范
前端
声声codeGrandMaster2 小时前
Vue入门2
前端·vue.js·vue
爱吃水果和蔬菜丫2 小时前
解决sortablejs+el-table表格内限制回撤和拖拽回撤失败问题
前端