Vuejs 动态绑定属性

v-bind 基础用法

<div id='app'>
  <!-- 错误做法,mustache只能用在content中 -->
  <img src="{{imgURL}}">
  <img src="imgURL">  

  <!-- 正确做法,使用v-bind指令 -->
  <img v-bind:src="imgURL">
  
  <!-- 语法糖:简写 -->
  <img :src="imgURL">
  <a :href="aHref">百度</a>
</div>

<script>
  const app = new Vue({
    el: '#app',
    data: {
      message: 'hello',
      imgURL: 'https://i0.hdslb.com/bfs/archive/e62b6b095ef38dfb742687f11e4b570dde420b5d.png',
      aHref: 'http://www.baidu.com'
    }
  })
</script>

v-bind 动态绑定class(对象语法)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Vuejs</title>
    <script src="js/vue.js"></script>
    <style>
      .active{
        color: red;
      }
      .font{
        font-size: 50px;
      }
    </style>
</head>
<body>
<div id='app'>
  <h2 class="active">{{message}}</h2>
  <!-- v-bind 传入对象 -->
  <!-- <h2 :class="{key1: val1, key2: val2}">{{message}}</h2> -->
  <!-- <h2 :class="{类名1: 布尔值1, 类名2: 布尔值2}">{{message}}</h2> -->
  <!-- <h2 :class="{active: isActive, font: isFont}">{{message}}</h2> -->

  <!-- <h2 :class="{active: isActive, font: isFont}">{{message}}</h2> -->
  <!-- 如果过于复杂,可以放在一个method或者computed中 -->
  <h2 :class="getClass()">{{message}}</h2>
  <button v-on:click = "btnClick">按钮</button>
</div>

<script>
  const app = new Vue({
    el: '#app',
    data: {
      message: 'hello',
      isActive: 'true',
      isFont: 'true'
    },
    methods: {
      btnClick: function(){
        this.isActive = !this.isActive
      },
      getClass: function(){
        return {active: this.isActive, font: this.isFont};
      }
    }
  })
</script>

</body>
</html>

v-bind 动态绑定class(数组语法)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Vuejs</title>
    <script src="js/vue.js"></script>
    <style>
      .active{
        color: red;
      }
      .font{
        font-size: 50px;
      }
    </style>
</head>
<body>
<div id='app'>
  <h2 class="active">{{message}}</h2>
  <!-- 传入数组 -->
  <!-- <h2 :class="[active, font]">{{message}}</h2> -->
  <h2 :class="getClass()">{{message}}</h2>
</div>

<script>
  const app = new Vue({
    el: '#app',
    data: {
      message: 'hello',
      active: 'aaaa',
      font: 'bbbb'
    },
    methods: {
      getClass: function(){
        return [this.active, this.font];
      }
    }
  })
</script>

</body>
</html>

v-bind 动态绑定style(对象语法)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Vuejs</title>
    <script src="js/vue.js"></script>
</head>
<body>
<div id='app'>
  <!-- v-bind动态绑定style 传入对象方式 -->
  <!-- '50px' 必须加上单引号,否则当成变量 -->
  <h2 :style="{fontSize: '50px', color: 'red'}">{{message}}</h2>
</div>

<script>
  const app = new Vue({
    el: '#app',
    data: {
      message: 'hello'
    }
  })
  
</script>

</body>
</html>

v-bind 动态绑定style(数组语法)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Vuejs</title>
    <script src="js/vue.js"></script>
</head>
<body>
<div id='app'>
  <!-- v-bind动态绑定style 传入数组方式 -->
  <h2 :style="[basesStyle1, basesStyle2]">{{message}}</h2>
</div>

<script>
  const app = new Vue({
    el: '#app',
    data: {
      message: 'hello',
      basesStyle1: {fontSize: '50px'},
      basesStyle2: {color: 'red'}
    }
  })
  
</script>

</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值