码迷,mamicode.com
首页 > 其他好文 > 详细

VUE 入门基础(5)

时间:2016-12-22 20:10:20      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:fonts   styles   static   bsp   java   active   com   个数   表示   

五,Class 与 Style 绑定

绑定HTML class

  对象语法

    我们可以传给v-bind:class 一个对象,以动态的切换class

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

    上面的语法表示 classactive 的更新将取决于数据属性 isActive 是否为真 。

      <div class=”static” v-bind:class=”{active:isActive,’text-danger’:hasError:false}”></div>

  渲染为:

    <div class=’static active’></div>

      也可以直接绑定数据里的一个对象

    <div v-bind:class=“classOject”></div>

    data: {

      classObject: {

      active: true,

      ‘text-danger’:false

      }

    }

  绑定返回对象的计算属性

    <div v-bind:class=”classObject”></div>

      data: {

        isActive:true,

        Error:null

      },

     computed: {

        classObject: function() {

        classObject:function(){

      return {

          active: this.isActive && !This.error,

          ‘text-danger’: this.error && this.error.type === ‘fatal’

        }

      }

    }

  }

  数组语法

    可以把一个数组传给 v-bind:class ,以应用一个 class 列表:

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

        data: {

          activeClass:’active’,

          errorClass:t’text-danger’

        }

    渲染为:

      <div class=”actvie text-danger”></div>

        如果你也想根据条件切换列表中的 class ,可以用三元表达式:

      <div v-bind:class="[isActive ? activeClass : ‘‘, errorClass]">

    用在组建上

      当你在一个定制的组件上用到 class 属性的时候,这些类将被添加到根元素上面,这个元素上已经存在的类不会被覆盖。

     如果你声明了这个组件

      Vue.component(‘my-component‘, {

          template: ‘<p class="foo bar">Hi</p>‘

      })

    然后在使用它的时候添加一些 class:

      <my-component class="baz boo"></my-component>

    HTML 最终将被渲染成为:

      <p class="foo bar baz boo">Hi</p>

    同样的适用于绑定 HTML class :

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

    当 isActive 为 true 的时候,HTML 将被渲染成为:

      <p class="foo bar active"></p>

    绑定内联样式

    v-bind:style 的对象语法十分直观 非常像css 其实它是一个javaScript对象css属性名可以用驼峰或短横分割命名

      <div v-bind:style=”{color:activeColor,fontSize:fontSize + ‘px’}”></div>

        data: {

            activeColor:’red’,

            fontSize:30

        }

    直接绑定到一个样式对象通常更好,让模板更清晰:

        <div v-bind:style="styleObject"></div>

        data: {

            styleObject: {

                color: ‘red‘,

                fontSize: ‘13px‘

            }

        }

  数组语法

    当v-bind:style 的数组语法可以将多个样式对象应用到一个元素上

      <div v-bind:style=“[baseStyle,overridingStyles]”>

VUE 入门基础(5)

标签:fonts   styles   static   bsp   java   active   com   个数   表示   

原文地址:http://www.cnblogs.com/nmxs/p/6212064.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!