猿问

img的src如何绑定v-for循环的变量值?

1.现有一v-for循环,输出每个对象comment,comment用element卡片组件分别输出其各个属性值

2.comment有一属性comsimghead存放图片名称,首先用v-if判断是否为空,若为空,img src="../images/head/head.jpg",这个没有问题,但是不为空怎么让src为"../images/head/"+comsimghead呢?(实现全部转换太麻烦,并且转换为后src=后面也不能写变量值啊,还得在data声明变量)

3.代码如下:


            <el-col :span="12" v-for="comment in currentcomments" :key="comment.comid" :offset="1" style="margin-bottom:40px">

                    <el-card :body-style="{ padding: '0px', height:'200px'}" style="width: 800px;height: 200px;">

                        <div style="padding: 6px;height: 180px;">

                            <div style="height:50px;"><!--头像与用户名-->

                                <div style="float: left;width: 50px;height: 50px;" v-if="comment.comsimghead ==null || comment.comsimghead ==''">

                                    <img src="../images/head/head.jpg" style="height: 40px;width: 40px;border-radius: 20px;margin: 10px 0px 10px 10px;" />

                                </div>

                                <div style="float: left;width: 50px;height: 50px;" v-else><!--这里的src怎么设置?-->

                                    <img src="../images/head/" style="height: 40px;width: 40px;border-radius: 20px;margin: 10px 0px 10px 10px;" />

                                </div>

                                <div style="float: left;position:relative;top:20px;width:300px;height: 30px;"><font size="5">&nbsp;{{comment.comsname}}</font></div>

                            </div>

                        </div>

                    </el-card>

                </el-col>


喵喵时光机
浏览 1661回答 3
3回答

慕无忌1623718

不需要使用v-if,v-else<div style="float: left;width: 50px;height: 50px;">&nbsp; &nbsp; &nbsp;<img :src='comment.comsimghead?"../images/head/head.jpg":("../images/head/"+comsimghead)' style="height: 40px;width: 40px;border-radius: 20px;margin: 10px 0px 10px 10px;" /></div>

蝴蝶不菲

<img :src="'../images/head/'+comment.comsimghead" style="height: 40px;width: 40px;border-radius: 20px;margin: 10px 0px 10px 10px;" />
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答