当前位置: 移动技术网 > IT编程>脚本编程>vue.js > 详解VueJs中的V-bind指令

详解VueJs中的V-bind指令

2018年05月11日  | 移动技术网IT编程  | 我要评论

向能青,最强大脑贾立平作弊,山水mp3

引子

v-bind  主要用于属性绑定,vue官方提供了一个简写方式 :bind,例如:

<!-- 完整语法 -->
<a v-bind:href="url"></a>
<!-- 缩写 -->
<a :href="url"></a>

  一、概述

      v-bind  主要用于属性绑定,比方你的class属性,style属性,value属性,href属性等等,只要是属性,就可以用v-bind指令进行绑定。

      示例:

<!-- 绑定一个属性 -->
<img v-bind:src="imagesrc">
<!-- 缩写 -->
<img :src="imagesrc">
<!-- 内联字符串拼接 -->
<img :src="'/path/to/images/' + filename">
<!-- class 绑定 -->
<div :class="{ red: isred }"></div>
<div :class="[classa, classb]"></div>
<div :class="[classa, { classb: isb, classc: isc }]">
<!-- style 绑定 -->
<div :style="{ fontsize: size + 'px' }"></div>
<div :style="[styleobjecta, styleobjectb]"></div>
<!-- 绑定一个有属性的对象 -->
<div v-bind="{ id: someprop, 'other-attr': otherprop }"></div>
<!-- 通过 prop 修饰符绑定 dom 属性 -->
<div v-bind:text-content.prop="text"></div>
<!-- prop 绑定。“prop”必须在 my-component 中声明。-->
<my-component :prop="something"></my-component>
<!-- 通过 $props 将父组件的 props 一起传给子组件 -->
<child-component v-bind="$props"></child-component>
<!-- xlink -->
<svg><a :xlink:special="foo"></a></svg>

二、绑定 html class

对象语法

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

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

      上面的语法表示 active 这个 class 存在与否将取决于数据属性 isactive 的 truthiness

你可以在对象中传入更多属性来动态切换多个 class。此外,v-bind:class 指令也可以与普通的 class 属性共存。当有如下模板:

<div class="static"
  v-bind:class="{ active: isactive, 'text-danger': haserror }">
</div>
  和如下 data

data: {
 isactive: true,
 haserror: false
}

       结果渲染为:

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

    当 isactive 或者 haserror 变化时,class 列表将相应地更新。例如,如果 haserror 的值为 true,class 列表将变为 "static active text-danger"

      绑定的数据对象不必内联定义在模板里

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

   渲染为:

<div class="active text-danger"></div>

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

<div v-bind:class="[isactive ? activeclass : '', errorclass]"></div>

    这样写将始终添加 errorclass,但是只有在 isactive 是 truthy 时才添加 activeclass。

     不过,当有多个条件 class 时这样写有些繁琐。所以在数组语法中也可以使用对象语法

<div v-bind:class="[{ active: isactive }, errorclass]"></div>

三、用在组件上

        当在一个自定义组件上使用 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>

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

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

   当 isactive 为 truthy时,html 将被渲染成为

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

 四、绑定内联样式

 对象语法

  v-bind:style 的对象语法十分直观——看着非常像 css,但其实是一个 javascript 对象。css 属性名可以用驼峰式 (camelcase) 或短横线分隔 (kebab-case,记得用单引号括起来) 来命名: 

<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="[basestyles, overridingstyles]"></div>

总结

以上所述是小编给大家介绍的vuejs中的v-bind指令,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网