• 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]”>

  • 相关阅读:
    给C盘瘦身的秘诀
    电化学词汇大全
    Scrapy学习-(1)
    数学部分分支关系总结,有修改的欢迎留言。
    BindingException: Mapper method 'xxx.dao.StudentDao.insertStudent' attempted to return null from a method with a primitive return type (int).
    insertSale attempted to return null from a method with a primitive return type (int).
    Maven [ERROR] 不再支持源选项 5。请使用 7 或更高版本
    Python中字符串型数组--转换为-->数字型数组
    18、pywintypes.com_error: (-2147221008, '尚未调用 coinitialize。', none, none)
    17、ModuleNotFoundError: No module named 'pywin32_bootstrap'
  • 原文地址:https://www.cnblogs.com/nmxs/p/6212064.html
Copyright © 2020-2023  润新知