uni-app入门-实战教程-十九-条件渲染

v-if 满足条件 view才显示

<view class="box-item" v-if = "isshow">1</view>

v-show 同 v-if ,相当于 v-if 增加了 隐藏样式

<view class="box-item" v-show = "isshow">1</view>

{{age >30 ? '中年人' : '青少年' }} 三元运算符,如果满足条件就是第一个,不满足条件等于二个引号内容

 <view class="box-item" v-if="(age > 20)">{{age}}{{age >30 ? '中年人' : '青少年' }}</view>

v-if v-else-if else 判断性别使用

        <view class="box">
            <view class="box-item" v-if="1">男</view>
            <view class="box-item" v-else-if="2">女</view>
            <view class="box-item" else="3">未知</view>
        </view>
        <button @tap="changeSex()" class="button">修改性别</button>
<template>
    <view>
        <view class="box">
            <view class="box-item" v-if="isshow">1</view>
        </view>
        <button @tap="changeShow()" class="button">{{bttext}}</button>
        
        <!-- v-show 隐藏显示 -->
        <view class="box">
            <view class="box-item" v-show="isshow">1</view>
        </view>
        <button @tap="changeShow()" class="button">{{bttext}}</button>
        
        
        <view class="box">
            <view class="box-item" v-if="(age > 20)">{{age}}{{age >30 ? '中年人' : '青少年' }}</view>
        </view>
        <button @tap="changeage()" class="button">修改年龄</button>
        
        
    </view>
</template>

<script>
    export default {
        data() {
            return {
                title: 'Hello',
                isshow:true,
                bttext: "隐藏",
                age: 18,
            }
        },
        onLoad() {

        },
        methods: {
            changeShow:function(){
                /* 如果issshow不等于 ture,那就赋值为 ture; */
                this.isshow =! this.isshow;
                if(this.isshow){
                    this.bttext = "隐藏";
                }else{
                    this.bttext = "显示";
                }
            },
            changeage:function(){
                this.age += 20;
            },
        }
    }
</script>

<style>
    .box{
        width: 100%;
        height: 500upx;
        border: 1upx solid #CCCCCC;
        /* 水平排列 */
        display: flex;      /* 自动换行 */
        /* flex-wrap: wrap-reverse; */
        /* 居中对齐 */
        justify-content: center;
        /* 垂直居中 */
        align-items: center;
    }
    
    
    .box-item{
        color: #FFFFFF;
        height: 200upx;
        width: 200upx;
        /* 行高 */
        line-height: 200upx;
        font-size: 30upx;
        /* 加粗 */
        font-weight: bold;
        text-align: center;
    }
    .box-item:nth-last-of-type(even){
        background: #007AFF;
    }
    .box-item:nth-last-of-type(odd){
        background: #09bb07;
    }
    .button{
        color: #007AFF;
    }
</style>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,505评论 0 13
  • 概要 64学时 3.5学分 章节安排 电子商务网站概况 HTML5+CSS3 JavaScript Node 电子...
    阿啊阿吖丁阅读 9,315评论 0 3
  • importUIKit classViewController:UITabBarController{ enumD...
    明哥_Young阅读 3,890评论 1 10
  • 查看滚动条的滚动距离 windox.pageXOffset/pageYOffset ie8及IE8以下不兼容 do...
    常婧帅阅读 63评论 0 0
  • 一觉醒来,头疼欲裂,又一次做梦回到了高中,埋头做在那没完没了的试卷,又看不清,解不出,压抑而焦灼。 每次做...
    风波会永恒_阅读 258评论 0 0