|
237 | 237 |
|
238 | 238 | ## v-bind
|
239 | 239 |
|
240 |
| -- **Shorthand:** `:` |
| 240 | +- **Shorthand:** `:` or `.` (when using `.prop` modifier) |
241 | 241 |
|
242 | 242 | - **Expects:** `any (with argument) | Object (without argument)`
|
243 | 243 |
|
|
246 | 246 | - **Modifiers:**
|
247 | 247 |
|
248 | 248 | - `.camel` - transform the kebab-case attribute name into camelCase.
|
| 249 | + - `.prop` - force a binding to be set as DOM property. <Badge text="3.2+"/> |
| 250 | + - `.attr` - force a binding to be set as DOM attribute. <Badge text="3.2+"/> |
249 | 251 |
|
250 | 252 | - **Usage:**
|
251 | 253 |
|
|
278 | 280 | <!-- class binding -->
|
279 | 281 | <div :class="{ red: isRed }"></div>
|
280 | 282 | <div :class="[classA, classB]"></div>
|
281 |
| - <div :class="[classA, { classB: isB, classC: isC }]"> |
282 |
| - <!-- style binding --> |
283 |
| - <div :style="{ fontSize: size + 'px' }"></div> |
284 |
| - <div :style="[styleObjectA, styleObjectB]"></div> |
| 283 | + <div :class="[classA, { classB: isB, classC: isC }]"></div> |
285 | 284 |
|
286 |
| - <!-- binding an object of attributes --> |
287 |
| - <div v-bind="{ id: someProp, 'other-attr': otherProp }"></div> |
| 285 | + <!-- style binding --> |
| 286 | + <div :style="{ fontSize: size + 'px' }"></div> |
| 287 | + <div :style="[styleObjectA, styleObjectB]"></div> |
288 | 288 |
|
289 |
| - <!-- prop binding. "prop" must be declared in my-component. --> |
290 |
| - <my-component :prop="someThing"></my-component> |
| 289 | + <!-- binding an object of attributes --> |
| 290 | + <div v-bind="{ id: someProp, 'other-attr': otherProp }"></div> |
291 | 291 |
|
292 |
| - <!-- pass down parent props in common with a child component --> |
293 |
| - <child-component v-bind="$props"></child-component> |
| 292 | + <!-- prop binding. "prop" must be declared in my-component. --> |
| 293 | + <my-component :prop="someThing"></my-component> |
294 | 294 |
|
295 |
| - <!-- XLink --> |
296 |
| - <svg><a :xlink:special="foo"></a></svg> |
297 |
| - </div> |
| 295 | + <!-- pass down parent props in common with a child component --> |
| 296 | + <child-component v-bind="$props"></child-component> |
| 297 | + |
| 298 | + <!-- XLink --> |
| 299 | + <svg><a :xlink:special="foo"></a></svg> |
| 300 | + ``` |
| 301 | + |
| 302 | + When setting a binding on an element, Vue by default checks whether the element has the key defined as a property using an `in` operator check. If the property is defined, Vue will set the value as DOM property instead of an attribute. This should work in most cases, but you can override this behavior by explicitly using `.prop` or `.attr` modifiers. This is sometimes necessary especially when [working with custom elements](/guide/web-components.html#passing-dom-properties). |
| 303 | + |
| 304 | + The `.prop` modifier also has a dedicated shorthand, `.`: |
| 305 | + |
| 306 | + ```html |
| 307 | + <div :someProperty.prop="someObject"></div> |
| 308 | + |
| 309 | + <!-- equivalent to --> |
| 310 | + <div .someProperty="someObject"></div> |
298 | 311 | ```
|
299 | 312 |
|
300 | 313 | The `.camel` modifier allows camelizing a `v-bind` attribute name when using in-DOM templates, e.g. the SVG `viewBox` attribute:
|
|
475 | 488 | This directive is provided solely for micro optimizations in performance-critical scenarios and should be rarely needed. The most common case where this may prove helpful is when rendering large `v-for` lists (where `length > 1000`):
|
476 | 489 |
|
477 | 490 | ```html
|
478 |
| - <div |
479 |
| - v-for="item in list" |
480 |
| - :key="itme.id" |
481 |
| - v-memo="[item.id === selected]" |
482 |
| - > |
| 491 | + <div v-for="item in list" :key="itme.id" v-memo="[item.id === selected]"> |
483 | 492 | <p>ID: {{ id }} - selected: {{ item.id === selected }}</p>
|
484 | 493 | <p>...more child nodes</p>
|
485 | 494 | </div>
|
|
0 commit comments