2020-11-11 Vue-03动态绑定属性

14-v-bind

<body>
  <div id="app">
    <h2>Mamba :{{name}}</h2>
    <img v-bind:src="imgURL" alt="">
    <a v-bind:href="aHref" alt="">百度一下</a>
  </div>
  <script src="../js/vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        name: 'Kobe Bryant',
        imgURL:'https://bkimg.cdn.bcebos.com/pic/83025aafa40f4bfb4f019c66044f78f0f736182a?x-bce-process=image/resize,m_lfit,w_480,limit_1',
        aHref: 'http://www.baidu.com'
      }
    })
  </script>
</body>

 

 

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

  <style>
    .active{
      color: #f40;
    }
    .line{
      font-size: xx-large;
    }
  </style>

<body>
  <div id="app">
    <h2 class='title' v-bind:class="{active: isActive, line: isLine}">{{name}}</h2>
    <button v-on:click="btnClick">按钮</button>
  </div>
  <script src="../js/vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        name: 'Kobe Bryant',
        isActive: true,
        isLine: true
      },
      methods: {
        btnClick: function(){
          this.isActive = !this.isActive;
          this.isLine = !this.isLine;
        }
      }
    })
  </script>
</body>

 

 

 

16-v-bind动态绑定class(数组语法)- 用的少

用法四methods示例:

<body>
  <div id="app">
    <!-- <h2 class='title' v-bind:class="{active: isActive, line: isLine}">{{name}}</h2> -->
    <h2 class='title' :class="getClasses()">{{name}}</h2>
    <button v-on:click="btnClick">按钮</button>
  </div>
  <script src="../js/vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        name: 'Kobe Bryant',
        isActive: true,
        isLine: true
      },
      methods: {
        btnClick: function(){
          this.isActive = !this.isActive;
          this.isLine = !this.isLine;
        },
        getClasses: function(){
          return {active: this.isActive, line: this.isLine}
        }
      }
    })
  </script>
</body>

 

<body>
  <div id="app">
    <h2 class='title' :class="['active', 'line']">{{name}}</h2>
  </div>
  <script src="../js/vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        name: 'Kobe Bryant',
      }
    })
  </script>
</body>

作业:

 

18-19-v-bind动态绑定style

<body>
  <div id="app">
    <h2 :style="{fontSize: finalSize + 'px', color: finalColor}">{{name}}</h2>
    <h2 :style="getStyles()">{{name}}</h2>  // 如果觉得上面太长的话,可以这样绑定
  </div>
  <script src="../js/vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        name: 'Kobe Bryant',
        finalSize: 100,
        finalColor: 'red'
      },
      methods: {
        getStyles: function(){
          return {fontSize: this.finalSize + 'px', color: this.finalColor}
        }
      }
    })
  </script>
</body>

数组

<body>
  <div id="app">
    <h2 :style="[baseStyle, baseStyle1]">{{name}}</h2>
  </div>
  <script src="../js/vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        name: 'Kobe Bryant',
        baseStyle: {color: 'red'},
        baseStyle1: {fontSize: '100px'}
      },
    })
  </script>
</body>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值