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 分钟前
【stm32】【printf】
java·前端·stm32
JIngJaneIL12 分钟前
智慧物业|物业管理|基于SprinBoot+vue的智慧物业管理系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·论文·智慧物业管理系统
爬坑的小白14 分钟前
vue 2.0 路由跳转时新开tab
前端·javascript·vue.js
爬坑的小白15 分钟前
vue x 状态管理
前端·javascript·vue.js
凌览29 分钟前
一键去水印|5 款免费小红书解析工具推荐
前端·javascript·后端
有意义30 分钟前
栈数据结构全解析:从实现原理到 LeetCode 实战
javascript·算法·编程语言
lichong95133 分钟前
鸿蒙 web组件开发
前端·typescript
1024小神34 分钟前
在html中使用js动态交换两个元素的位置
前端
鹿鹿鹿鹿isNotDefined35 分钟前
逐步手写,实现符合 Promise A+ 规范的 Promise
前端·javascript·算法
一千柯橘35 分钟前
Electron - IPC 解决主进程和渲染进程之间的通信
前端