vuejs v-bind给元素添加属性

一、动态绑定一个或多个常见属性(css属性),例如class,style等

<!-- 绑定一个 attribute -->            <img v-bind:src="imageSrc">

<!-- 动态 attribute 名 (2.6.0+) -->          <button v-bind:[key]="value"></button>

<!-- 内联字符串拼接 -->        <img :src="'/path/to/images/' + fileName">

<!-- 通过 prop 修饰符绑定 DOM attribute -->        <div v-bind:text-content.prop="text"></div>

修饰符

  • .prop - 作为一个 DOM property 绑定而不是作为 attribute 绑定。
  • .camel - (2.1.0+) 将 kebab-case attribute 名转换为 camelCase。(从 2.1.0 开始支持)
  • .sync (2.3.0+) 语法糖,会扩展成一个更新父组件绑定值的 v-on 侦听器。

下面具体来看:

1、通过HTML class 绑定

--- 对象语法:

传给v-bind:class 一个对象,以动态地切换 class:

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

 active是class类名,isActive为布尔值;

更多字段来动态切换多个 class,如:

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

v-bind:class 指令也可以与普通的 class attribute 共存。

data中的isActive、hasError对象:

data: {
  isActive:true,
  hasError: true
}

结果渲染为:

绑定的数据对象不必内联定义在模板里,可直接定义一个对象,如:

<div v-bind:class="classObject"></div>
data: {
  classObject: {
    active: true,
    'text-danger': false
  }
}

也可以在这里绑定一个返回对象的计算属性,如:

<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'
    }
  }
}

--- 数组语法:

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

<div v-bind:class="[activeClass, errorClass]"></div>
data: {
  activeClass: 'active',
  errorClass: 'text-danger'
}

渲染为:

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

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

  isActive为true时显示activeClass类,为false时显示errorClass类。

当有多个条件 class 时,在数组语法中也可以使用对象语法,如:

<div v-bind:class="[{ active: isActive }, errorClass]"></div>
data: {
  isActive: true,
  errorClass: 'text-danger'
}

--- 用在组件上:

在一个自定义组件上使用 class property 时,这些 class 将被添加到该组件的根元素上面。这个元素上已经存在的 class 不会被覆盖。

例如,如果你声明了这个组件:

Vue.component('my-component', {
  template: '<p class="foo bar">Hi</p>'
})

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

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

HTML 将被渲染为:

对于带数据绑定 class 也同样适用,如:

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

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

2、绑定内联样式

--- 对象语法:

v-bind:style 的对象语法十分直观——看着非常像 CSS,但其实是一个 JavaScript 对象

如:

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

activeColor、fontSize 是一个对象:

data: {
  activeColor: 'red',
  fontSize: 30
}

直接绑定一个对象,如:

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

data中的styleObject对象:

data: {
  styleObject: {
    color: 'red',
    fontSize: '13px'
  }
}

--- 数组语法:

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

如:

<div v-bind:style="[baseStyles, overridingStyles]"></div>
data:{
    baseStyles:{
      backgroundColor:'green',
        color:'#ffffff'
      },
      overridingStyles:{
        borderRadius:'20px',
        'font-weight': 'bold'
      },
}

--- 自动添加前缀:

当 v-bind:style 使用需要添加浏览器引擎前缀的 CSS property 时,如 transform,Vue.js 会自动侦测并添加相应的前缀。

--- 多重值:

从 2.3.0 起你可以为 style 绑定中的 property 提供一个包含多个值的数组,常用于提供多个带前缀的值,例如:

<div :style="{ display: ['-webkit-box', '-ms-flexbox', 'flex'] }"></div>

这样写只会渲染数组中最后一个被浏览器支持的值。在本例中,如果浏览器支持不带浏览器前缀的 flexbox,那么就只会渲染 display: flex

谷歌显示:

猜你喜欢

转载自blog.csdn.net/weixin_42220533/article/details/110124149