-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11-class-and-style-bindings.html
74 lines (63 loc) · 1.81 KB
/
11-class-and-style-bindings.html
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Vue</title>
</head>
<body>
<div id="app">
<div class="static" v-bind:class="{ active: isActive, 'text-danger': hasError }">
class="static" v-bind:class="{ active: isActive, 'text-danger': hasError }<br>
This element's class is "static active" from data
</div>
<br>
<div v-bind:class="classObject">
v-bind:class="classObject"<br>
This element's class is "text-danger" from computed
</div>
<br>
<div v-bind:class="[activeClass, errorClass]">
v-bind:class="[activeClass, errorClass]"<br>
This element's class is "active text-danger"<br>
We can pass an array to v-bind:class to apply a list of classes:
</div>
<br>
<div v-bind:class="[isActive ? activeClass : '', errorClass]">
v-bind:class="[isActive ? activeClass : '', errorClass]"<br>
This element's class is "active text-danger"<br>
toggle a class in the list conditionally, you can do it with a ternary expression:
</div>
<br>
<div v-bind:class="[{ active: isActive }, errorClass]">
v-bind:class="[{ active: isActive }, errorClass]"<br>
This element's class is "active text-danger"<br>
Another syntax for above
</div>
</div>
<script src="https://unpkg.com/vue@2.1.10/dist/vue.js"></script>
<script>
//https://vuejs.org/v2/guide/class-and-style.html
var error = new Error();
error.type = 'fatal';
new Vue({
el: '#app',
data: {
isActive: true,
hasError: false,
error: error,
//error: null,
activeClass: 'active',
errorClass: 'text-danger'
},
computed: {
classObject: function () {
return {
active: this.isActive && !this.error,
'text-danger': this.error && this.error.type === 'fatal',
}
}
}
})
</script>
</body>
</html>