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 just 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.
7
+
Una necesidad común para usar `v-bind` con los datos es manipular la lista de clases de un elemento, así como sus estilos en línea. Como ambos son atributos, podemos usar `v-bind`para controlarlos: sólo debemos calcular un string final con nuestras expresiones. Sin embargo, trabajar con concatenaciones de strings es molesto y propenso a errores. Por esta razón, Vue ofrece mejoras especiales cuando`v-bind`es usado junto con `class` and `style`. En adición a strings, las expresiones pueden también ser evaluadas a objetos o arrays.
8
8
9
-
## Binding HTML Classes
9
+
## V-bind en Clases HTML
10
10
11
-
### Object Syntax
11
+
### Sintaxis de Objeto
12
12
13
-
We can pass an object to `v-bind:class`to dynamically toggle classes:
13
+
Podemos pasar un objeto a `v-bind:class`para intercambiar dinámicamente las clases:
14
14
15
15
```html
16
16
<divv-bind:class="{ active: isActive }"></div>
17
17
```
18
18
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`.
19
+
La anterior sintaxis ilustra que la presencia de la clase `active`será determinada por el [thrutiness](https://developer.mozilla.org/en-US/docs/Glossary/Truthy)de la propiedad`isActive`.
20
20
21
-
You can have multiple classes toggled by having more fields in the object. In addition, the `v-bind:class`directive can also co-exist with the plain `class` attribute. So given the following template:
21
+
Puede intercambiar múltiples clases si tiene más campos en el objeto. Adicionalmente, la directiva `v-bind:class`puede coexistir con el atributo común `class`. De modo que en la siguiente plantilla:
When `isActive`or`hasError`changes, the class list will be updated accordingly. For example, if`hasError`becomes `true`, the class list will become `"static active text-danger"`.
44
+
Siempre que `isActive`o`hasError`cambie, la lista de clases será actualizada acordemente. Por ejemplo, si`hasError`toma el valor `true`, la lista de clases cambiará a `"static active text-danger".`
45
45
46
-
The bound object doesn't have to be inline:
46
+
El objeto asignado a `v-bind` no necesariamente debe ser en líenea:
47
47
48
48
```html
49
49
<divv-bind:class="classObject"></div>
@@ -57,7 +57,7 @@ data: {
57
57
}
58
58
```
59
59
60
-
This will render the same result. We can also bind to a [computed property](computed.html)that returns an object. This is a common and powerful pattern:
60
+
Esto mostrará el mismo resultado. También podemos usar `v-bind` con una [propiedad calculada](computed.html)que retorne un objeto. Esto es un patrón muy común y poderoso:
61
61
62
62
```html
63
63
<divv-bind:class="classObject"></div>
@@ -77,9 +77,9 @@ computed: {
77
77
}
78
78
```
79
79
80
-
### Array Syntax
80
+
### Sintaxis de Array
81
81
82
-
We can pass an array to`v-bind:class`to apply a list of classes:
82
+
Podemos pasar un array a`v-bind:class`para aplicar una lista de clases:
83
83
84
84
```html
85
85
<divv-bind:class="[activeClass, errorClass]">
@@ -91,69 +91,69 @@ data: {
91
91
}
92
92
```
93
93
94
-
Which will render:
94
+
Lo cual renderizará:
95
95
96
96
```html
97
97
<divclass="active text-danger"></div>
98
98
```
99
99
100
-
If you would like to also toggle a class in the list conditionally, you can do it with a ternary expression:
100
+
Si quisiera también intercambiar una clase en el listado de clases condicionalmente, lo puede hacer con una expresión ternaria:
This will always apply `errorClass`, but will only apply `activeClass`when`isActive`is`true`.
106
+
Esto siempre aplicará `errorClass`, pero solo aplicará `activeClass`cuando`isActive`tenga valor`true`.
107
107
108
-
However, this can be a bit verbose if you have multiple conditional classes. That's why it's also possible to use the object syntax inside array syntax:
108
+
Sin embargo, esto puede ser un poco extenso si tiene múltiples clases condicionales. Por esto también es posible usar la sintaxis de objeto dentro de la sintaxis de array:
> This section assumes knowledge of [Vue Components](components.html). Feel free to skip it and come back later.
116
+
> Esta sección asume conocimientos previos de [Componentes Vue](components.html). Sé libre de saltar ésta sección y regresar más adelante.
117
117
118
-
When you use the`class`attribute on a custom component, those classes will be added to the component's root element. Existing classes on this element will not be overwritten.
118
+
Cuando usa el atributo`class`en un componente personalizado, aquellos cambios serán adicionados al elemento raíz del componente. Las clases existentes de dicho elemento no serán sobreescritas.
When`isActive`is truthy, the rendered HTML will be:
146
+
Cuando`isActive`es verdadero, el HTML renderizado será:
147
147
148
148
```html
149
149
<pclass="foo bar active"></p>
150
150
```
151
151
152
-
## Binding Inline Styles
152
+
## V-bind en estilos en línea
153
153
154
-
### Object Syntax
154
+
### Sintaxis de objeto
155
155
156
-
The object syntax for `v-bind:style`is pretty straightforward - it looks almost like CSS, except it's a JavaScript object. You can use either camelCase or kebab-case for the CSS property names:
156
+
La sintaxis de objetos para `v-bind:style`es bastante sencillo - casi parece CSS, excepto que es un objeto javascript. Puede usar camelCase o kebab-case para los nombres de propiedades:
When you use a CSS property that requires vendor prefixes in`v-bind:style`, for example`transform`, Vue will automatically detect and add appropriate prefixes to the applied styles.
194
+
Cuando usa una propiedad CSS que requiera prefijos de navegador en`v-bind:style`, por ejemplo`transform`, Vue automáticamente detectará y agregará los prefijos apropiados a los estilos aplicados.
0 commit comments