You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vue中v-bind:src绑定图片URL不显示问题求助

问题分析与解决

核心错误:数组访问方式错误

你的myData是数组类型,但模板里直接使用myData.image——数组本身没有image属性,导致绑定的src值为undefined,图片自然无法加载,浏览器也不会抛出明显报错。

修复方案

方案1:遍历数组渲染所有图片

如果要展示数组里的全部图片,用v-for循环遍历myData:

<MyComp v-if="myData.length"> 
    <template #img> 
        <div class="my-slot"> 
            <img v-for="item in myData" :key="item.image" :src="item.image"/> 
        </div> 
    </template> 
</MyComp>

方案2:渲染数组中指定项的图片

如果只需要展示某一项(比如第一项)的图片,通过索引直接访问:

<MyComp v-if="myData.length"> 
    <template #img> 
        <div class="my-slot"> 
            <img :src="myData[0].image"/> 
        </div> 
    </template> 
</MyComp>

额外排查点(针对静态URL也无效的情况)

  • 检查MyComp组件内部是否正确定义了名为img的插槽:确保组件里存在<slot name="img"></slot>代码,否则插槽内容不会被渲染。
  • 验证静态URL的有效性:直接在浏览器地址栏打开该URL,确认图片能正常加载,排除资源本身不可访问的问题。

内容的提问来源于stack exchange,提问作者dityan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 20:20:18