zoukankan      html  css  js  c++  java
  • VUE 入门基础(5)

    五,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]”>

  • 相关阅读:
    关于url路径的定义方式
    script标签的位置
    jQuery中ready与load事件的区别
    Python中cv2库和matplotlib库色彩空间排布不一致
    OpenCV for Python 学习笔记 三
    OpenCV for Python 学习笔记 二
    OpenCV for Python 学习笔记 一
    CentOS7配置opencv for python && eclipse c/c++[更新]
    <转>关于 error LNK2019:无法解析的外部符号 ,该符号在函数**中被引用的思考
    OpenCV load 运行出错 cv::Exception 出错
  • 原文地址:https://www.cnblogs.com/nmxs/p/6212064.html
Copyright © 2011-2022 走看看