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


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

    效果

    代码 

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

    1. <template>
    2. <view>
    3. <image :src="getImagePath(line1)" alt="">image>
    4. <image :src="getImagePath(line2)" alt="">image>
    5. <image :src="getImagePath(line3)" alt="">image>
    6. view>
    7. template>
    8. <script>
    9. export default {
    10. data() {
    11. return {
    12. down: getApp().globalData.icon + 'index/edit.png',
    13. up: getApp().globalData.icon + 'index/ing.png',
    14. line: getApp().globalData.icon + 'index/none_bind.png',
    15. line1:'photo1',
    16. line2:'photo2',
    17. line3:'photo3',
    18. }
    19. },
    20. methods: {
    21. //图片展示
    22. getImagePath(rate) {
    23. console.log(rate)
    24. if (rate === "photo1") {
    25. return this.line;
    26. } else if (rate === "photo2") {
    27. return this.down;
    28. } else {
    29. return this.up;
    30. }
    31. },
    32. },
    33. onLoad(){
    34. },
    35. }
    36. script>
    37. <style lang="scss">
    38. image{
    39. width:50px;
    40. height:50px;
    41. }
    42. style>

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

    效果

    代码

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

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

    1. <template>
    2. <view>
    3. <image :src="info ==50 ? img1 : img2" alt="">image>
    4. <image :src="info1 == -50 ? img1 : img2" alt="">image>
    5. view>
    6. template>
    7. <script>
    8. export default {
    9. data() {
    10. return {
    11. info:50,
    12. info1:-50,
    13. img1: getApp().globalData.icon + 'index/exit.png',
    14. img2: getApp().globalData.icon + 'index/edit.png',
    15. };
    16. }
    17. };
    18. script>
    19. <style>
    20. image{
    21. width:50px;
    22. height:50px;
    23. }
    24. style>
  • 相关阅读:
    MyBatis缓存机制
    降本增效这九个月,爱奇艺从“穿越火线”,到“冷静增长”
    List 实现
    如何有效提升你的论证写作能力?
    外包干了2个月,技术退步明显.......
    AI在线工具分享
    15:00面试,15:08就出来了,问的问题有点变态。。。
    腾讯广告可直跳淘宝天猫,双11流量抢占不可错过!
    C++——string
    PyTorch中的matmul函数详解
  • 原文地址:https://blog.csdn.net/weixin_46001736/article/details/132871569