代码干货 | class与style绑定

news/2024/7/10 0:21:13 标签: class, html, 数组, vue, 对象绑定
html" title=class>class="baidu_pl">
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 列表:


>>>展开全文


http://www.niftyadmin.cn/n/1452615.html

相关文章

think big,make different

1、我对新东西是总是持谨慎的态度&#xff0c;创新工场的刚起步&#xff0c;还需要磨练&#xff0c;而且从现阶段的情况看下来&#xff0c;并不太妙。百度的技术还是很不错的&#xff0c;人家好多年了&#xff0c;用户数量也很大&#xff0c;也有很多积累了&#xff0c;所以还是…

MAKEWORD(2,2)解释

宏的原始定义&#xff1a;#define MAKEWORD(a, b) ((WORD)(((BYTE)(((DWORD_PTR)(a)) & 0xff)) | ((WORD)((BYTE)(((DWORD_PTR)(b)) & 0xff))) << 8)) 返回值&#xff1a;一个无符号16位整形数。 例子&#xff1a; WORD wVersionRequested; wVersionRequested…

技术文章 | 前端开发中的柯里化

本文来源于阿里云-云栖社区&#xff0c;原文点击这里。 引用维基百科的定义&#xff1a; 在计算机科学中&#xff0c;柯里化&#xff08;英语&#xff1a;Currying&#xff09;&#xff0c;又译为卡瑞化或加里化&#xff0c;是把接受多个参数的函数变换成接受一个单一参数&…

【翻译】使用Ext JS设计响应式应用程序

原文&#xff1a;Designing Responsive Applications with Ext JS在当今这个时代&#xff0c;用户都希望Web应用程序无论在形状还是大小上&#xff0c;既能在桌面电脑&#xff0c;也能在移动设备上使用。使应用程序能适应不同的需求渐成趋势。幸运的是&#xff0c;Ext JS 5提供…

TCP/IP编程中htons()编程

htons是将整型变量从主机字节顺序转变成网络字节顺序&#xff0c; 就是整数在地址空间存储方式变为&#xff1a;高位字节存放在内存的低地址处。 网络字节顺序是TCP/IP中规定好的一种数据表示格式&#xff0c;它与具体的CPU类型、操作系统等无关&#xff0c;从而可以保证数据在…

代码干货 | vue.js列表渲染

本文来源于阿里云-云栖社区&#xff0c;原文点击这里。 v-for 我们用 v-for 指令根据一组数组的选项列表进行渲染。 v-for 指令需要以 item in items 形式的特殊语法&#xff0c; items 是源数据数组并且 item 是数组元素迭代的别名 基本用法 <ul id"example-1"&…

inet_ntoa函数

将一个十进制网络字节序转换为点分十进制IP格式的字符串。

移动微社区----附手机端滑动分页源码

背景&#xff1a;楼主目前在一家做电子商务的公司上班&#xff0c;公司已有成熟的商城系统&#xff0c;社区系统&#xff0c;以及CMS框架。 而做社区的&#xff0c;最大的就是腾讯。今年大概三月份的时候&#xff0c;腾讯推出了微社区。推出了就推出了吧&#xff0c;这也没啥。…