1. 程式人生 > >vue樣式繫結

vue樣式繫結

<div v-bind:class="{ active: isActive }"></div>

<div class="static"
     v-bind:class="{ active: isActive, 'text-danger': hasError }">
</div>
<div id="app">
  <div v-bind:class="classObject"></div>
</div>


new Vue({
  el: '#app',
  data: {
  isActive: true,
  error: null
  },
  computed: {
    classObject: function () {
      return {
        active: this.isActive && !this.error,
        'text-danger': this.error && this.error.type === 'fatal',
      }
    }
  }
})

<div v-bind:class="[activeClass, errorClass]"></div>

errorClass 是始終存在的,isActive 為 true 時新增 activeClass 類:

<divv-bind:class="[errorClass ,isActive ? activeClass : '']"></div>
<div id="app">
    <div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }">菜鳥教程</div>
</div>
<div id="app">
  <div v-bind:style="styleObject">菜鳥教程</div>
</div>


<script>
new Vue({
  el: '#app',
  data: {
    styleObject: {
      color: 'green',
      fontSize: '30px'
    }
  }
})
<div id="app">
  <div v-bind:style="[baseStyles, overridingStyles]">菜鳥教程</div>
</div>


<script>
new Vue({
  el: '#app',
  data: {
    baseStyles: {
      color: 'green',
      fontSize: '30px'
    },
overridingStyles: {
      'font-weight': 'bold'
    }
  }
})
</script>