前端框架 VUE-class绑定-style绑定和条件渲染(三)

class绑定

  • :class=‘xxx’
  • 表达式是字符串: ‘classA’
  • 表达式是对象: {classA:isA, classB: isB}
  • 表达式是数组: [‘classA’, ‘classB’]

style绑定

  • :style="{ color: activeColor, fontSize: fontSize + ‘px’ }"
  • 其中 activeColor/fontSize 是 data 属性

条件渲染指令

  • v-if v-else v-show
比较v-if 和 v-show
  • 如果需要频繁切换 v-show 较好
  • 当条件不成立时, v-if 的所有子节点不会解析(项目中使用)

具体代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<style>
    .aClass{
        color:red;
    }
    .bClass{
        color: green;
    }
    .cClass{
        font-size: 50px;
    }
    .dClass{
        color: aquamarine;
    }

</style>

<script src="vue.min.js"></script>
<body>
<!-- class绑定   :class -->
<div id="demo">

    <p class="cClass" :class="a">class绑定->字符串</p>
    <p :class="{aClass:boola, bClass:boolb}">class绑定->对象</p>
    <p :class="[classX,classV]">class绑定->数组</p>

    <p :style="{color:activeColor,fontSize:fontSize + 'px'}">style绑定</p>

    <button @click="update">更新</button>

    <hr />


    <p v-if="ok">成功</p>
    <p v-else>失败</p>


    <p v-show="ok">升职成功</p>
    <p v-show="!ok">升职失败</p>


    <button @click="ok=!ok">变</button>


</div>

</body>

<script type="text/javascript">

    new Vue({

        el:'#demo',
        data: {
            a:'aClass',
            boola: true,
            boolb: false,
            classX:'aClass',
            classV:'cClass',
            activeColor:'red',
            fontSize:'20',
            ok:true,
        },
        methods:{
            update(){
                this.a = 'bClass';
                this.boola = false;
                this.boolb = true;
                this.classX = 'bClass';
                this.classV = 'dClass';
                this.activeColor = 'yellow';
                this.fontSize='60';
            }
        },

    })


</script>

</html>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值