当前位置:网站首页>v-for动态设置img的src
v-for动态设置img的src
2022-07-26 09:25:00 【44w0】
项目开发中遇到这个问题,记录总结一下。
问题:利用v-for动态设置图片,图片路径处写法如下图,发现图片不显示
<div v-for="(item,index) in classify" :key="index" class="cart">
<img :src="item.img" alt="">
<div>
<p>{
{item.title}}</p>
<p>{
{nums[index]}}</p>
</div>
</div>
//遍历的数据:
data () {
return {
classify:[
{
title:"项目总数",
img:require('@/assets/images/point/1.png')
},
{
title:"总积分",
img:require('@/assets/images/point/2.png')
},
{
title:"总工作积分",
img:require('@/assets/images/point/3.png')
},
{
title:"总奖励积分",
img:require('@/assets/images/point/4.png')
}
]
}
},
查了一下,错误在:
- 在遍历的数组中img写的是路径,但是for循环遍历的时候img的src会将该路径解析为字符串,而不是路径,导致路径错误
解决方案:
- 在数组中img赋值时,在路径前边加上require(),之后img的 src就不会将此解析为字符串了
在这里插入代码片data () {
return {
classify:[
{
title:"项目总数",
img:require('@/assets/images/point/1.png')
},
{
title:"总积分",
img:require('@/assets/images/point/2.png')
},
{
title:"总工作积分",
img:require('@/assets/images/point/3.png')
},
{
title:"总奖励积分",
img:require('@/assets/images/point/4.png')
}
]
}
},```
边栏推荐
猜你喜欢

NTT (fast number theory transformation) polynomial inverse 1500 word analysis

Paper notes: knowledge map kgat (unfinished temporary storage)

Windows backs up the database locally by command

异常处理机制二

Stm32+mfrc522 completes IC card number reading, password modification, data reading and writing

arc-gis的基本使用2

JVM command induction

MySQL transaction

Mysql事务

js中树与数组的相互转化(树的子节点若为空隐藏children字段)
随机推荐
Solve "note: one or more layouts are missing the layout_width or layout_height attributes."
2022 mobile crane driver test question simulation test question bank simulation test platform operation
Original root and NTT 5000 word explanation
2022 tea artist (intermediate) special operation certificate examination question bank simulated examination platform operation
Local cache
keepalived 实现mysql自动故障切换
Object 的Wait Notify NotifyAll 源码解析
Processing of inconsistent week values obtained by PHP and MySQL
搜索模块用例编写
Jmeter配置元件之CSV数据文件设置
JS output diamond on the console
docker配置mysql集群
cocoapods的安装和使用
tabbarController的使用
神经网络与深度学习-6- 支持向量机1 -PyTorch
What are CSDN spaces represented by
Apple generated and verified tokens for PHP
Arc GIS basic operation 3
Paper notes: knowledge map kgat (unfinished temporary storage)
When you click input, the border is not displayed!