You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A common need for data binding is manipulating an element's class list and its inline styles. Since they are both attributes, we can use `v-bind` to handle them: we only need to calculate a final string with our expressions. However, meddling with string concatenation is annoying and error-prone. For this reason, Vue provides special enhancements when `v-bind` is used with `class` and `style`. In addition to strings, the expressions can also evaluate to objects or arrays.
@@ -14,16 +18,18 @@ We can pass an object to `:class` (short for `v-bind:class`) to dynamically togg
14
18
15
19
The above syntax means the presence of the `active` class will be determined by the [truthiness](https://developer.mozilla.org/en-US/docs/Glossary/Truthy) of the data property `isActive`.
16
20
17
-
You can have multiple classes toggled by having more fields in the object. In addition, the `:class` directive can also co-exist with the plain `class` attribute. So given the following template:
21
+
You can have multiple classes toggled by having more fields in the object. In addition, the `:class` directive can also co-exist with the plain `class` attribute. So given the following state:
We can pass an array to `:class` to apply a list of classes:
88
138
89
-
```vue-html
90
-
<div :class="[activeClass, errorClass]"></div>
139
+
<divclass="composition-api">
140
+
141
+
```js
142
+
constactiveClass=ref('active')
143
+
consterrorClass=ref('text-danger')
91
144
```
92
145
146
+
</div>
147
+
148
+
<divclass="options-api">
149
+
93
150
```js
94
151
data() {
95
152
return {
@@ -99,6 +156,12 @@ data() {
99
156
}
100
157
```
101
158
159
+
</div>
160
+
161
+
```vue-html
162
+
<div :class="[activeClass, errorClass]"></div>
163
+
```
164
+
102
165
Which will render:
103
166
104
167
```vue-html
@@ -121,26 +184,22 @@ However, this can be a bit verbose if you have multiple conditional classes. Tha
121
184
122
185
### With Components
123
186
124
-
> This section assumes knowledge of [Vue Components](/guide/essentials/component-basics). Feel free to skip it and come back later.
125
-
126
-
When you use the `class` attribute on a custom component with a single root element, those classes will be added to this element. Existing classes on this element will not be overwritten.
187
+
> This section assumes knowledge of [Components](/guide/essentials/component-basics). Feel free to skip it and come back later.
127
188
128
-
For example, if you declare this component:
189
+
When you use the `class` attribute on a component with a single root element, those classes will be added to the component's root element, and merged with any existing class already on it.
129
190
130
-
```js
131
-
constapp=Vue.createApp({})
191
+
For example, if we have a component named `my-component` with the following template:
132
192
133
-
app.component('my-component', {
134
-
template:`<p class="foo bar">Hi!</p>`
135
-
})
193
+
```vue-html
194
+
<!-- child component template -->
195
+
<p class="foo bar">Hi!</p>
136
196
```
137
197
138
198
Then add some classes when using it:
139
199
140
200
```vue-html
141
-
<div id="app">
142
-
<my-component class="baz boo"></my-component>
143
-
</div>
201
+
<!-- when using the component -->
202
+
<my-component class="baz boo"></my-component>
144
203
```
145
204
146
205
The rendered HTML will be:
@@ -164,20 +223,20 @@ When `isActive` is truthy, the rendered HTML will be:
164
223
If your component has multiple root elements, you would need to define which component will receive this class. You can do this using `$attrs` component property:
165
224
166
225
```vue-html
167
-
<div id="app">
168
-
<my-component class="baz"></my-component>
169
-
</div>
226
+
<!-- my-component template using $attrs -->
227
+
<p :class="$attrs.class">Hi!</p>
228
+
<span>This is a child component</span>
170
229
```
171
230
172
-
```js
173
-
constapp=Vue.createApp({})
231
+
```vue-html
232
+
<my-component class="baz"></my-component>
233
+
```
174
234
175
-
app.component('my-component', {
176
-
template:`
177
-
<p :class="$attrs.class">Hi!</p>
178
-
<span>This is a child component</span>
179
-
`
180
-
})
235
+
Will render:
236
+
237
+
```html
238
+
<pclass="baz">Hi!</p>
239
+
<span>This is a child component</span>
181
240
```
182
241
183
242
You can learn more about component attribute inheritance in [Non-Prop Attributes](/guide/components/attrs.html) section.
@@ -188,10 +247,17 @@ You can learn more about component attribute inheritance in [Non-Prop Attributes
188
247
189
248
The object syntax for `:style` is pretty straightforward - it looks almost like CSS, except it's a JavaScript object. You can use either camelCase or kebab-case (use quotes with kebab-case) for the CSS property names:
0 commit comments