Vue.js 动态为img的src赋值
需求是这样:ajax获取数据如下{"code": "200","data": {"SumAmount": 200,"List": [{"amount": 100,"sex": "male","fee": 1,"id": 98,
·
需求是这样:
ajax获取数据如下
{
"code": "200",
"data": {
"SumAmount": 200,
"List": [{
"amount": 100,
"sex": "male",
"fee": 1,
"id": 98,
"status": 2,
"time": "2015-08-11"
}, {
"amount": 100,
"sex": "female",
"fee": 0,
"id": 8,
"status": 2,
"time": "2015-06-12"
}]
},
"msg": "success"
}
然后渲染列表到页面,如果男,则将img的src设为"images/male.png",反之设为"images/female.png"
两个都可以实现,为了在html中看起来舒服点还是用filter吧,虽然也就一个判断逻辑,但是判断语句加上url,这就不美观了,当然,这只是个人习惯,直接用指令的话直观点
<img v-attr="src: sex=='male'?'images/male.png':'images/female.png'">
<img v-attr="src: sex | isM">
对应的filter
filters: {
isM: function (val) {
return val == 'male' ? 'images/male.png' : 'images/female.pn'
}
}
方法很多,我写我推荐的吧:
首先男女这样的标示属于装饰性内容,我建议写到css里面。也就是说用背景图的形式来控制现实男女
这样你有两个class .male
female
<span class={{sex}}></span>
更多推荐
已为社区贡献1条内容
所有评论(0)