Vue#Class 与 Style 绑定
绑定HTMLCLASS
在我没看这之前,我觉得要写绑定class ,应该像绑定数据一样这么写
class ={{class-a}}
看官方教程时,不推荐这么写,推荐这样
v-bind:class="{ 'class-a': isA, 'class-b': isB }"
官方的解释,我觉得还是挺接地气的,最起码我能看的懂。
数据绑定一个常见需求是操作元素的 class 列表和它的内联样式。因为它们都是属性,我们可以用 v-bind 处理它们:我们只需要计算出表达式最终的字符串。不过,字符串拼接麻烦又易错。因此,在v-bind 用于 class 和 style 时,Vue.js 专门增强了它。表达式的结果类型除了字符串之外,还可以是对象或数组。
可以用对象语法来处理它们:
可以这样:https://jsfiddle.net/miloer/36ek1uco/
也可以这样:https://jsfiddle.net/miloer/36ek1uco/1/
也可以使用数组语法来处理:
https://jsfiddle.net/miloer/36ek1uco/2/
我觉得在样式里用表达式判断,这么做挺有创意的,不过个人感觉这么做又繁琐了点,不过官方说:
当有多个条件 class 时这样写有些繁琐。在 1.0.19+ 中,可以在数组语法中使用对象语法:
<div v-bind:class="[classA, { classB: isB, classC: isC }]">
这样是不是好多了。
绑定内联样式:
这个和绑定HTMLCLASS 方法差不多。
https://jsfiddle.net/miloer/36ek1uco/3/
https://jsfiddle.net/miloer/36ek1uco/4/
自动添加前缀
这个我觉得挺方便的,当使用v-bind:style 需要添加前缀CSS时,Vue.js 会自动侦测并添加相应的前缀。

v-bind
- 缩写:
: - 类型:
* (with argument) | Object (without argument) - 参数:
attrOrProp (optional) - 修饰符:用法:动态地绑定一个或多个 attribute,或一个组件 prop 到表达式。
.sync- 双向绑定,只能用于 prop 绑定。.once- 单次绑定,只能用于 prop 绑定。.camel- 将绑定的特性名字转回驼峰命名。只能用于普通 HTML 特性的绑定,通常用于绑定用驼峰命名的 SVG 特性,比如viewBox。
在绑定
class或style时,支持其它类型的值,如数组或对象。在绑定 prop 时,prop 必须在子组件中声明。可以用修饰符指定不同的绑定类型。
没有参数时,可以绑定到一个对象。注意此时
class和style绑定不支持数组和对象。- 示例:
<!-- 绑定 attribute -->
<img v-bind:src="data:imageSrc"> <!-- 缩写 -->
<img :src="data:imageSrc"> <!-- 绑定 class -->
<div :class="{ red: isRed }"></div>
<div :class="[classA, classB]"></div>
<div :class="[classA, { classB: isB, classC: isC }]"></div> <!-- 绑定 style -->
<div :style="{ fontSize: size + 'px' }"></div>
<div :style="[styleObjectA, styleObjectB]"></div> <!-- 绑定到一个对象 -->
<div v-bind="{ id: someProp, 'other-attr': otherProp }"></div> <!-- prop 绑定,"prop" 必须在 my-component 组件内声明 -->
<my-component :prop="someThing"></my-component> <!-- 双向 prop 绑定 -->
<my-component :prop.sync="someThing"></my-component> <!-- 单次 prop 绑定 -->
<my-component :prop.once="someThing"></my-component> - 另见:
Vue#Class 与 Style 绑定的更多相关文章
- vue class与style绑定、条件渲染、列表渲染
列表渲染 根据我例子的需要,先来说下,列表渲染使用到的是v-for指令,需要使用 item in items 形式的特殊语法,items 是源数据数组并且 item 是数组元素迭代的别名,具体使用方法 ...
- vue class与style 绑定详解——小白速会
一.绑定class的几种方式 1.对象语法 直接看例子: <div id="app3"> <div :class="{'success':isSucce ...
- Vue 使用v-bind:style 绑定样式
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Vue - class与style绑定
1.通过v-bind绑定一个class A:直接绑定 B:通过v-bind绑定一个对象 C:绑定一个返回对象的计算属性 D:绑定一个数组对象 2.绑定内联样式 A:直接子啊属性名上面绑定 B:绑定到一 ...
- 关于vue.js中class与style绑定的学习
练习代码: html: <!DOCTYPE html><html lang="en"><head> <meta charset=" ...
- Vue中class与style绑定
gitHub地址:https://github.com/lily1010/vue_learn/tree/master/lesson07 一 用对象的方法绑定class 很简单,举个栗子: <!D ...
- Vue.2.0.5-Class 与 Style 绑定
Class 与 Style 绑定 数据绑定一个常见需求是操作元素的 class 列表和它的内联样式.因为它们都是属性 ,我们可以用v-bind 处理它们:只需要计算出表达式最终的字符串.不过,字符串拼 ...
- Vue学习4:class与style绑定
说明:有些部分我只是相当于做一个学习笔记,加强记忆之用.所以可能阅读性不是那么强.如果有参考我这类博客的人,那么请见谅. 代码如下: <!DOCTYPE html> <html la ...
- vue从入门到进阶:Class 与 Style 绑定(四)
绑定 HTML Class 对象语法 ①.添加单个class: <div v-bind:class="{ active: isActive }"></div> ...
随机推荐
- POJ 1088
http://poj.org/problem?id=1088 一道中文题,这道题如果不限时的话,是个简单的搜索,但限时的话,就要用记忆化搜索 所谓记忆化搜索就是对每一次搜索的结果进行记录,然后之后的如 ...
- c++转义字符、指针
上篇博客的答案: 1: // DataTypeDemo.cpp : 定义控制台应用程序的入口点. 2: // 3: 4: #include "stdafx.h" 5: #incl ...
- 如何在maven中添加本地jar包
mvn install:install-file -DgroupId=mytest-DartifactId=test-Dversion=1.1 -Dpackaging=jar -Dfile=d:\te ...
- ABAP ALV单个单元格状态编辑-简单版本
*&---------------------------------------------------------------------* *& Report ZPPR0024 ...
- 【leetcode】triangle(easy)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- 【leetcode】Restore IP Addresses (middle)
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- IOS-frame和bounds有什么不同
frame指的是:该view在父view坐标系统中的位置和大小.(参照点是父亲的坐标系统) 它的坐标原点是随着父View位置的改变而改变的 bounds指的是:该view在本身坐标系统中 的位置和大小 ...
- 基于SSH2的OA项目1.0_20161206_需求分析与框架搭建
1. SSH项目 OA项目,办公自动化,将公司的数据,文档,流程实现在系统中的管理. 降低人员交流过程中的成本.提高办公的效率. 2 .系统管理 主要实现系统权限的管理,不同的用户登陆后看到菜单项不一 ...
- XP/Win7下QTP11循环试用30天的破解方法
XP/Win7下QTP11循环试用30天的破解方法. XP下:1.找到以下路径:C:\Documents and Settings\All Users\Application Data\SafeNet ...
- September 7th 2016 Week 37th Wednesday
Patience is bitter, but its fruit is sweet. 忍耐是痛苦的,但它的果实却是甜蜜的. However, many may give up before they ...