示例代码:
<template>
<div>
<div v-for="(item,index) in product">
<img src="{{ item.imgSrc }}" alt="">
</div>
</div>
</template>
发现这样运行会报错
img标签动态绑定src
<template>
<div>
<div v-for="(item,index) in product">
<img :src="item.imgSrc" alt="">
</div>
</div>
</template>
完整代码如下:
<template>
<div>
<div v-for="(item,index) in product">
<img :src="item.imgSrc" alt="">
</div>
</div>
</template>
<script>
export default {
name: "test",
data() {
return {
product:[
{
id:"1",
imgSrc:"http://www.py3study.com/Public/images/article/thumb/random/158.jpg",
},
{
id:"2",
imgSrc:"http://www.py3study.com/Public/images/article/thumb/random/159.jpg",
},
{
id:"3",
imgSrc:"http://www.py3study.com/Public/images/article/thumb/random/160.jpg",
},
],
};
},
}
</script>
<style scoped>
</style>
总结:主要是图片位置的问题,图片存放在什么位置,能够让img通过src动态的绑定对应的值
其次是图片配置路径的问题,若图片就和响应的vue在同一个文件,直接通过 ./logo.png 是只能写死的情况下显示,而动态的:src绑定是无法完成这种情况的显示。
本文参考链接:
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有