前端水很深

vue.js 样式绑定

2017-12-19  本文已影响67人  张小小小七

style(外联样式)

语句: v-bind:class="classStyle";

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

<script>
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',
      }
    }
  }
})

<style>
.active {
    width: 100px;
    height: 100px;
    background: green;
}
.text-danger {
    background: red;
}
</style>

<div id="app">
    <div v-bind:class="[errorClass ,isActive ? activeClass : '']"></div>
</div>

<script>
new Vue({
  el: '#app',
  data: {
    isActive: true,
    activeClass: 'active',
    errorClass: 'text-danger'
  }
})
</script>

style(内联样式)

语句: v-bind:style="style";

<div id="app">
    <div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }">哈哈哈</div>
</div>
上一篇下一篇

猜你喜欢

热点阅读