Guide: Class and Style Bindings Flashcards
What is the reasoning behind the special enhancements for class
and style
?
- Meddling with string concatenation is annoying, error-prone, and not structured
- Using
v-bind
with theclass
andstyle
attributes enables us to have expressions evaluate to objects or arrays, along with strings - Therefore, strings, objects, and arrays are valid values for
class
andstyle
Describe the various ways you can use object syntax with v-bind:class
.
- By passing an object literal
- `
`
- By using it with the plain
class
attribute- `
`
- By passing a reference to an object
- `
`
* define `classObj` in `data`, or from `props` * By binding to a computed property which returns an object * Super powerful and common
Describe the ways to use array syntax.
- String literals in the array
- `
`
- Bind to properties
- `
`
- Toggling an individual class conditionally with a ternary expression
- `
`
- Use object syntax inside of array syntax
- `
`
What happens when using 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.
Describe how to properly type style property names with binded style
attributes.
- Use either camelCase or kebab-case with quotes
- `
`
- `
What is the best way to use object syntax with the style
attribute.
To either bind to a style object, or a computed property, so that the template doesn’t get too bloated.
Describe how to use the array syntax for the style
attribute
- Allows you to bind multiple style objects to the same element
- `
`
- `
What special operation happens when using :style
?
Vue automatically detects and adds appropriate prefixes that are required for specific browsers.