uni-app:实现条件判断展示图片(函数判定+三目运算)

一、多条件判断(通过函数进行图片展示)

效果

代码 

在data中定义图片信息和要传递的数据信息,在src中写入函数并携带要传递的数据,通过传递的数据在函数中进行判断,并返回对应的图片信息

<template>
	<view>
		<image :src="getImagePath(line1)" alt=""></image>
		<image :src="getImagePath(line2)" alt=""></image>
		<image :src="getImagePath(line3)" alt=""></image>
  </view>
</template>
<script>
export default {
	  data() {
		return {
			down: getApp().globalData.icon + 'index/edit.png',
			up: getApp().globalData.icon + 'index/ing.png',
			line: getApp().globalData.icon + 'index/none_bind.png',
			line1:'photo1',
			line2:'photo2',
			line3:'photo3',
		}
	  },
	methods: {
		//图片展示
		getImagePath(rate) {
			console.log(rate)
		      if (rate === "photo1") {
		        return this.line;
		      } else if (rate === "photo2") {
		        return this.down;
		      } else {
		        return this.up;
		      }
		    },
	},
	onLoad(){
		
	},
}
</script>
<style lang="scss">
	image{
		width:50px;
		height:50px;
	}
</style>

二、三目运算判断一个条件

效果

代码

 三目运算(表达式?true:false)

下例:当变量info的值为50时执行变量img1,反之执行变量img2;当变量info1的值为-50时执行变量img1,反之执行变量img2。则这里满足info = 50,info1 = -50,则都执行:src="img1"

<template>
  <view>
    <image :src="info ==50 ? img1 : img2" alt=""></image>
	<image :src="info1 == -50 ? img1 : img2" alt=""></image>
  </view>
</template>

<script>
export default {
  data() {
    return {
      info:50,
	  info1:-50,
	  img1: getApp().globalData.icon + 'index/exit.png',
	  img2: getApp().globalData.icon + 'index/edit.png',
    };
  }
};
</script>
<style>
	image{
		width:50px;
		height:50px;
	}
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值