这次给大家带来vuejs中v-bind指令怎样使用,vuejs中v-bind指令使用的注意事项有哪些,下面就是实战案例,一起来看一下。
引子
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 绑定 -->
<p :class="{ red: isred }"></p>
<p :class="[classa, classb]"></p>
<p :class="[classa, { classb: isb, classc: isc }]">
<!-- style 绑定 -->
<p :style="{ fontsize: size + 'px' }"></p>
<p :style="[styleobjecta, styleobjectb]"></p>
<!-- 绑定一个有属性的对象 -->
<p v-bind="{ id: someprop, 'other-attr': otherprop }"></p>
<!-- 通过 prop 修饰符绑定 dom 属性 -->
<p v-bind:text-content.prop="text"></p>
<!-- 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
<p v-bind:class="{ active: isactive }"></p>
上面的语法表示 active 这个 class 存在与否将取决于数据属性 isactive 的 truthiness
你可以在对象中传入更多属性来动态切换多个 class。此外,v-bind:class 指令也可以与普通的 class 属性共存。当有如下模板:
<p class="static"
v-bind:class="{ active: isactive, 'text-danger': haserror }">
</p>
和如下 data
data: {
isactive: true,
haserror: false
}
结果渲染为:
<p class="static active"></p>
当 isactive 或者 haserror 变化时,class 列表将相应地更新。例如,如果 haserror 的值为 true,class 列表将变为 static active text-danger
绑定的数据对象不必内联定义在模板里
<p v-bind:class="classobject"></p>
data: {
classobject: {
active: true,
'text-danger': false
}
}
渲染的结果和上面一样。我们也可以在这里绑定一个返回对象的计算属性。这是一个常用且强大的模式:
<p v-bind:class="classobject"></p>
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 列表
<p v-bind:class="[activeclass, errorclass]"></p>
data: {
activeclass: 'active',
errorclass: 'text-danger'
}
渲染为:
<p class="active text-danger"></p>
如果你也想根据条件切换列表中的 class,可以用三元表达式
<p v-bind:class="[isactive ? activeclass : '', errorclass]"></p>
这样写将始终添加 errorclass,但是只有在 isactive 是 truthy 时才添加 activeclass。
不过,当有多个条件 class 时这样写有些繁琐。所以在数组语法中也可以使用对象语法
<p v-bind:class="[{ active: isactive }, errorclass]"></p>
三、用在组件上
当在一个自定义组件上使用 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,记得用单引号括起来) 来命名:
<p v-bind:style="{ color: activecolor, fontsize: fontsize + 'px' }"></p>
data: {
activecolor: 'red',
fontsize: 30
}
直接绑定到一个样式对象通常更好,这会让模板更清晰
<p v-bind:style="styleobject"></p>
data: {
styleobject: {
color: 'red',
fontsize: '13px'
}
}
同样的,对象语法常常结合返回对象的计算属性使用
数组语法
v-bind:style 的数组语法可以将多个样式对象应用到同一个元素上
<p v-bind:style="[basestyles, overridingstyles]"></p>
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
react router4+redux控制路由权限步骤详解
v-bind与v-on使用案例详解
vue表单类父子组件数据传递数据方法总结
以上就是vuejs中v-bind指令怎样使用的详细内容。