vue样式绑定

可以用v-bind绑定,只需要通过表达式计算出字符串结果即可。

1.绑定css   v-bind:class

对象语法

<div v-bind:class="{ active: isActive }"></div>
<!-- 可以与普通的css共存 -->
<div
  class="static"
  v-bind:class="{ active: isActive, 'text-danger': hasError }"
></div>

可以绑定一个对象的计算属性

<div v-bind:class="classObject"></div>
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>
<!-- 三元表达式 -->
<div v-bind:class="[isActive ? activeClass : '', errorClass]"></div>
<!-- 数组里面还可以使用对象 -->
<div v-bind:class="[{ active: isActive }, errorClass]"></div>
data: {
  activeClass: 'active',
  errorClass: 'text-danger'
}

绑定到该组件的根元素上

<my-component class="baz boo"></my-component>
<!-- 组件 -->
Vue.component('my-component', {
  template: '<p class="foo bar">Hi</p>'
})
<!-- 渲染结果结果 -->
<p class="foo bar baz boo">Hi</p>

2.绑定内联样式    v-bind:style

用法同上,css属性名采用驼峰命名或者短线分隔(要加引号)

<div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }"></div>

 

<!-- 还可以绑定 -->

posted @ 2019-03-06 13:58  #小小佳  阅读(135)  评论(0编辑  收藏  举报