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>

然后就好啦~

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

相关推荐
谢泽豪几秒前
解决 uniapp 修改index.html文件不生效的问题
前端·uni-app
袁煦丞几秒前
【黑科技指南】自托管私人导航站Dashy:cpolar内网穿透实验室第476个成功挑战
前端·程序员·远程工作
Nayana1 分钟前
Clean Code JavaScript小记(一)
javascript
郭少3 分钟前
🔥 放弃 vw!我在官网大屏适配中踩了天坑,用 postcss-px-to-viewport-8-plugin 实现了 Rem 终极方案
vue.js·性能优化·nuxt.js
heartmoonq4 分钟前
关于前端监控用户行为导致的报错
前端
已读不回1434 分钟前
告别痛苦的主题切换!用一个插件解决 Tailwind CSS 多主题开发的所有烦恼
前端·架构
咸虾米4 分钟前
微信小程序通过uni.chooseLocation打开地图选择位置,相关设置及可能出现的问题
vue.js·微信小程序
pepedd8645 分钟前
🚀Webpack 从入门到优化,一文全掌握!
前端·webpack·trae
TimelessHaze6 分钟前
【面试考点】从URL输入到页面展示
前端·trae
玲小珑7 分钟前
LangChain.js 完全开发手册(一)AI 应用开发入门
前端·langchain·ai编程