html" title=class>class="article_content clearfix">
html" title=class>class="
htmledit_views">
本文来源于阿里云-云栖社区,原文点击这里。
数据绑定一个常见需求是操作元素的 html" title=class>class 列表和它的内联样式。因为它们都是属性 ,我们可以用v-bind 处理它们:只需要计算出表达式最终的字符串。不过,字符串拼接麻烦又易错。因此,在 v-bind 用于 html" title=class>class 和 style 时, Vue.js 专门增强了它。表达式的结果类型除了字符串之外,还可以是对象或数组
绑定 html html" title=class>class
对象语法
我们可以传给 v-bind:html" title=class>class 一个对象,以动态地切换 html" title=class>class
html" title=class>class="hljs javascript" style="margin:0px; padding:0.5em; font-size:undefined; font-family:Menlo,Monaco,Consolas,"Courier New",monospace; color:rgb(248,248,242); background:rgb(35,36,31); white-space:pre-wrap; display:block; overflow-x:auto"><div v-bind:html" title=class>class="hljs-html" title=class>class" style="">html" title=class>class="hljs-keyword" style="color:rgb(102,217,239)">html" title=class>class=html" title=class>class="hljs-string" style="color:rgb(230,219,116)">"{ active: isActive }">html" title=class>class="xml" style="">html" title=class>class="hljs-tag" style=""></html" title=class>class="hljs-name" style="color:rgb(102,217,239)">div>
上面的语法表示 html" title=class>class的active 更新将取决于数据属性 isActive 是否为真值
我们也可以在对象中传入更多属性用来动态切换多个 html" title=class>class 。此外, v-bind:html" title=class>class 指令可以与普通的 html" title=class>class 属性共存:
html" title=class>class="hljs javascript" style="margin:0px; padding:0.5em; font-size:undefined; font-family:Menlo,Monaco,Consolas,"Courier New",monospace; color:rgb(248,248,242); background:rgb(35,36,31); white-space:pre-wrap; display:block; overflow-x:auto"><div html" title=class>class="hljs-html" title=class>class" style="">html" title=class>class="hljs-keyword" style="color:rgb(102,217,239)">html" title=class>class=html" title=class>class="hljs-string" style="color:rgb(230,219,116)">"static" v-bind:html" title=class>class="hljs-html" title=class>class" style="">html" title=class>class="hljs-keyword" style="color:rgb(102,217,239)">html" title=class>class=html" title=class>class="hljs-string" style="color:rgb(230,219,116)">"{ active: isActive, 'text-danger': hasError }">
html" title=class>class="xml" style="">html" title=class>class="hljs-tag" style=""></html" title=class>class="hljs-name" style="color:rgb(102,217,239)">div>
如下 data:
html" title=class>class="hljs yaml" style="margin:0px; padding:0.5em; font-size:undefined; font-family:Menlo,Monaco,Consolas,"Courier New",monospace; color:rgb(248,248,242); background:rgb(35,36,31); white-space:pre-wrap; display:block; overflow-x:auto">html" title=class>class="hljs-attr" style="">data: {
html" title=class>class="hljs-attr" style=""> isActive: html" title=class>class="hljs-literal" style="color:rgb(174,129,255)">true,
html" title=class>class="hljs-attr" style=""> hasError: html" title=class>class="hljs-literal" style="color:rgb(174,129,255)">false
}
渲染为:
html" title=class>class="hljs javascript" style="margin:0px; padding:0.5em; font-size:undefined; font-family:Menlo,Monaco,Consolas,"Courier New",monospace; color:rgb(248,248,242); background:rgb(35,36,31); white-space:pre-wrap; display:block; overflow-x:auto"><div html" title=class>class="hljs-html" title=class>class" style="">html" title=class>class="hljs-keyword" style="color:rgb(102,217,239)">html" title=class>class=html" title=class>class="hljs-string" style="color:rgb(230,219,116)">"static active">html" title=class>class="xml" style="">html" title=class>class="hljs-tag" style=""></html" title=class>class="hljs-name" style="color:rgb(102,217,239)">div>
当 isActive 或者 hasError 变化时,html" title=class>class 列表将相应地更新
你也可以直接绑定数据里的一个对象:
html" title=class>class="hljs applescript" style="margin:0px; padding:0.5em; font-size:undefined; font-family:Menlo,Monaco,Consolas,"Courier New",monospace; color:rgb(248,248,242); background:rgb(35,36,31); white-space:pre-wrap; display:block; overflow-x:auto"><html" title=class>class="hljs-keyword" style="color:rgb(249,38,114)">div v-bind:html" title=class>class="hljs-built_in" style="color:rgb(230,219,116)">html" title=class>class=html" title=class>class="hljs-string" style="color:rgb(230,219,116)">"html" title=class>classObject"></html" title=class>class="hljs-keyword" style="color:rgb(249,38,114)">div>
<html" title=class>class="hljs-keyword" style="color:rgb(249,38,114)">script>
data: {
html" title=class>classObject: {
active: html" title=class>class="hljs-literal" style="color:rgb(174,129,255)">true,
html" title=class>class="hljs-built_in" style="color:rgb(230,219,116)">text-danger': html" title=class>class="hljs-literal" style="color:rgb(174,129,255)">false
}
}
</html" title=class>class="hljs-keyword" style="color:rgb(249,38,114)">script>
渲染的结果和上面一样。我们也可以在这里绑定返回对象的计算属性。这是一个常用且强大的模式:
html" title=class>class="hljs applescript" style="margin:0px; padding:0.5em; font-size:undefined; font-family:Menlo,Monaco,Consolas,"Courier New",monospace; color:rgb(248,248,242); background:rgb(35,36,31); white-space:pre-wrap; display:block; overflow-x:auto"><html" title=class>class="hljs-keyword" style="color:rgb(249,38,114)">div v-bind:html" title=class>class="hljs-built_in" style="color:rgb(230,219,116)">html" title=class>class=html" title=class>class="hljs-string" style="color:rgb(230,219,116)">"html" title=class>classObject"></html" title=class>class="hljs-keyword" style="color:rgb(249,38,114)">div>
<html" title=class>class="hljs-keyword" style="color:rgb(249,38,114)">script>
data: {
isActive: html" title=class>class="hljs-literal" style="color:rgb(174,129,255)">true,
html" title=class>class="hljs-keyword" style="color:rgb(249,38,114)">error: null
},
computed: {
html" title=class>classObject: function () {
html" title=class>class="hljs-built_in" style="color:rgb(230,219,116)"> return {
active: this.isActive && !this.html" title=class>class="hljs-keyword" style="color:rgb(249,38,114)">error,
html" title=class>class="hljs-built_in" style="color:rgb(230,219,116)">text-danger': this.html" title=class>class="hljs-keyword" style="color:rgb(249,38,114)">error && this.html" title=class>class="hljs-keyword" style="color:rgb(249,38,114)">error.type === 'fatal',
}
}
}
</html" title=class>class="hljs-keyword" style="color:rgb(249,38,114)">script>
我们可以把一个数组传给 v-bind:html" title=class>class ,以应用一个 html" title=class>class 列表:
>>>展开全文