3.綁定屬性、綁定html、綁定class、綁定style

  • 2019 年 10 月 7 日
  • 筆記

1.綁定屬性

<template>    <div id="app">        <!-- 綁定屬性 -->      <br>      <div v-bind:title='title'>滑鼠懸浮出現</div>      <br>      <img :src="url" alt="仙女">        </div>  </template>    <script>  export default {    name: 'app',    data () {      return {        title:'滑鼠懸浮出現',        url:'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1569319252742&di=0eaf19ed1e01d10d7f612da9434599a6&imgtype=0&src=http%3A%2F%2Fimg3.duitang.com%2Fuploads%2Fitem%2F201603%2F28%2F20160328121906_ErzAB.thumb.700_0.jpeg'      }    }  }  </script>    <style>    </style>

2.綁定html

<template>    <div id="app">        <!-- html綁定 -->      <div v-html="h"></div>      </div>  </template>    <script>  export default {    name: 'app',    data () {      return {        h:'<h2>二級標題</h>'      }    }  }  </script>    <style>    </style>

 3.綁定class

<template>    <div id="app">        <!-- 綁定class -->      <div :class="{'red':flag,'blue':!flag}">綁定class</div>      <div :class="{'red':!flag,'blue':flag}">綁定class</div>        <ul>        <li v-for="(item,index) in list" :key=index :class="{'red':index==1,'blue':index==2}">{{item}}</li>      </ul>      </div>  </template>    <script>  export default {    name: 'app',    data () {      return {        flag:true,        list:['第一行','第二行','第三行']      }    }  }  </script>    <style>    .red{color: red}  .blue{color:blue}    </style>

4.綁定style

<template>    <div id="app">      <!-- 綁定style  -->      <div class="box" :style="{width:boxWidth+'px'}">這是一個div</div>      </div>  </template>  <script>  export default {    name: 'app',    data () {      return {        boxWidth:300      }    }  }  </script>  <style>  .box{    height: 100px;    width: 100px;    background-color: aquamarine;  }  </style>