当前位置: 首页 > news >正文

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>
http://www.lryc.cn/news/170041.html

相关文章:

  • http概念
  • Postman应用——Variable变量使用(Global、Environment和Collection)
  • php高级 TP+Redis实现发布订阅和消息推送案例实战
  • Python 基础入门
  • 【跟小嘉学 Rust 编程】二十九、Rust 中的零拷贝序列化解决方案(rkyv)
  • 路由器端口转发
  • Redis模块一:缓存简介
  • 【去除若依首页】有些小项目不需要首页,去除方法
  • Ardupilot — EKF3使用光流室内定位代码梳理
  • 【Linux】自动化构建工具 —— make/makefileLinux第一个小程序 - 进度条
  • tensorflow的unet模型
  • (2023 最新版)IntelliJ IDEA 下载安装及配置教程
  • react 实现拖动元素
  • 【EI会议】第二届声学,流体力学与工程国际学术会议(AFME 2023)
  • Android StringFog 字符串自动加密
  • 上四休三,未来的期许
  • 怎么防止360安全卫士修改默认浏览器?
  • 调整参数提高mysql读写速度
  • 第一届电子纸产业创新应用论坛-邀请函
  • Go expvar包
  • Yolo v8代码逐行解读
  • 9.18号作业
  • Spring源码阅读(spring-framework-5.2.24)
  • 【SpringMVC】文件上传与下载、JREBEL使用
  • 数据结构 第二章作业 线性表 西安石油大学
  • vue.mixin全局混合选项
  • VMware Fusion 13+Ubuntu ARM Server 22.04.3在M2芯片的Mac上共享文件夹
  • PostgreSQL serial类型
  • [创业之路-76] - 创业公司如何在长期坚持中顺势而为?诚迈科技参观交流有感
  • 人脸修复祛马赛克算法CodeFormer——C++与Python模型部署