Class 与 Style 绑定
操作元素的 class 列表和内联样式是数据绑定的一个常见需求。因为它们都是属性,所以我们可以用 v-bind
处理它们:只需要通过表达式计算出字符串结果即可。不过,字符串拼接麻烦且易错。因此,在将 v-bind
用于 class
和 style
时,Vue.js 做了专门的增强。表达式结果的类型除了字符串之外,还可以是对象或数组。
绑定 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"
。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 | <!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Vue中的样式绑定</title>
<script src="./vue.js"></script>
<style>
.activated {
color: red;
}
</style>
</head>
<body>
<div id="app">
<div @click="handleDivClick"
:class="{ activated: isActivated }"
>
hello world
</div>
</div>
<script>
var vm = new Vue({
el: "#app",
data: {
isActivated: false
},
methods: {
handleDivClick: function() {
this.isActivated = !this.isActivated;
}
}
})
</script>
</body>
</html>
|
绑定的数据对象不必内联定义在模板里:
| <div v-bind:class="classObject"></div>
|
| data: {
classObject: {
active: true,
'text-danger': false
}
}
|
渲染的结果和上面一样。我们也可以在这里绑定一个返回对象的计算属性。这是一个常用且强大的模式:
| <div v-bind:class="classObject"></div>
|
1
2
3
4
5
6
7
8
9
10
11
12 | 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>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 | <!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Vue中的样式绑定</title>
<script src="./vue.js"></script>
<style>
.activated {
color: red;
}
</style>
</head>
<body>
<div id="app">
<div @click="handleDivClick"
:class="[activated, activatedOne]"
>
hello world
</div>
</div>
<script>
var vm = new Vue({
el: "#app",
data: {
activated: "",
activatedOne: "activated-one",
},
methods: {
handleDivClick: function() {
this.activated = this.activated === "activated" ? "" :"activated";
}
}
})
</script>
</body>
</html>
|
如果你也想根据条件切换列表中的 class,可以用三元表达式:
| <div v-bind:class="[isActive ? activeClass : '', errorClass]"></div>
|
这样写将始终添加 errorClass
,但是只有在 isActive
是 truthy 时才添加 activeClass
。
不过,当有多个条件 class 时这样写有些繁琐。所以在数组语法中也可以使用对象语法:
| <div v-bind:class="[{ active: isActive }, errorClass]"></div>
|
用在组件上
当在一个自定义组件上使用 class 属性时,这些 class 将被添加到该组件的根元素上面。这个元素上已经存在的 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
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 | <!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Vue中的样式绑定</title>
<script src="./vue.js"></script>
</head>
<body>
<div id="app">
<div :style="styleObj" @click="handleDivClick">
hello world
</div>
</div>
<script>
var vm = new Vue({
el: "#app",
data: {
styleObj: {
color: "black"
}
},
methods: {
handleDivClick: function() {
this.styleObj.color = this.styleObj.color === "black" ? "red" : "black"
}
}
})
</script>
</body>
</html>
|
直接绑定到一个样式对象通常更好,这会让模板更清晰:
| <div v-bind:style="styleObject"></div>
|
| data: {
styleObject: {
color: 'red',
fontSize: '13px'
}
}
|
同样的,对象语法常常结合返回对象的计算属性使用。
数组语法
v-bind:style
的数组语法可以将多个样式对象应用到同一个元素上:
| <div v-bind:style="[baseStyles, overridingStyles]"></div>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 | <!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Vue中的样式绑定</title>
<script src="./vue.js"></script>
</head>
<body>
<div id="app">
<div :style="[styleObj, { fontSize: '20px' }]" @click="handleDivClick">
hello world
</div>
</div>
<script>
var vm = new Vue({
el: "#app",
data: {
styleObj: {
color: "black"
}
},
methods: {
handleDivClick: function() {
this.styleObj.color = this.styleObj.color === "black" ? "red" : "black"
}
}
})
</script>
</body>
</html>
|
自动添加前缀
当 v-bind:style
使用需要添加浏览器引擎前缀
的 CSS 属性时,如 transform
,Vue.js 会自动侦测并添加相应的前缀。
多重值
从 2.3.0 起你可以为 style
绑定中的属性提供一个包含多个值的数组,常用于提供多个带前缀的值,例如:
| <div :style="{ display: ['-webkit-box', '-ms-flexbox', 'flex'] }"></div>
|
这样写只会渲染数组中最后一个被浏览器支持的值。在本例中,如果浏览器支持不带浏览器前缀的 flexbox,那么就只会渲染 display: flex
。