|
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 a DOM property. <Badge text="3.2+"/> |
| 250 | + - `.attr` - force a binding to be set as a 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 a 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:
|
|
451 | 464 | </ul>
|
452 | 465 | ```
|
453 | 466 |
|
| 467 | + Since 3.2, you can also memoize part of the template with invalidation conditions using [`v-memo`](#v-memo). |
| 468 | + |
454 | 469 | - **See also:**
|
455 | 470 | - [Data Binding Syntax - interpolations](../guide/template-syntax.html#text)
|
| 471 | + - [v-memo](#v-memo) |
| 472 | + |
| 473 | +## v-memo <Badge text="3.2+" /> |
| 474 | + |
| 475 | +- **Expects:** `Array` |
| 476 | + |
| 477 | +- **Details:** |
| 478 | + |
| 479 | + Memoize a sub-tree of the template. Can be used on both elements and components. The directive expects a fixed-length array of dependency values to compare for the memoization. If every value in the array was the same as last render, then updates for the entire sub-tree will be skipped. For example: |
| 480 | + |
| 481 | + ```html |
| 482 | + <div v-memo="[valueA, valueB]"> |
| 483 | + ... |
| 484 | + </div> |
| 485 | + ``` |
| 486 | + |
| 487 | + When the component re-renders, if both `valueA` and `valueB` remain the same, all updates for this `<div>` and its children will be skipped. In fact, even the Virtual DOM VNode creation will also be skipped since the memoized copy of the sub-tree can be reused. |
| 488 | + |
| 489 | + It is important to specify the memoization array correctly, otherwise we may skip updates that should indeed be applied. `v-memo` with an empty dependency array (`v-memo="[]"`) would be functionally equivalent to `v-once`. |
| 490 | + |
| 491 | + **Usage with `v-for`** |
| 492 | + |
| 493 | + `v-memo` 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`): |
| 494 | + |
| 495 | + ```html |
| 496 | + <div v-for="item in list" :key="item.id" v-memo="[item.id === selected]"> |
| 497 | + <p>ID: {{ item.id }} - selected: {{ item.id === selected }}</p> |
| 498 | + <p>...more child nodes</p> |
| 499 | + </div> |
| 500 | + ``` |
| 501 | + |
| 502 | + When the component's `selected` state changes, a large amount of VNodes will be created even though most of the items remained exactly the same. The `v-memo` usage here is essentially saying "only update this item if it went from non-selected to selected, or the other way around". This allows every unaffected item to reuse its previous VNode and skip diffing entirely. Note we don't need to include `item.id` in the memo dependency array here since Vue automatically infers it from the item's `:key`. |
| 503 | + |
| 504 | + :::warning |
| 505 | + When using `v-memo` with `v-for`, make sure they are used on the same element. **`v-memo` does not work inside `v-for`.** |
| 506 | + ::: |
| 507 | + |
| 508 | + `v-memo` can also be used on components to manually prevent unwanted updates in certain edge cases where the child component update check has been de-optimized. But again, it is the developer's responsibility to specify correct dependency arrays to avoid skipping necessary updates. |
| 509 | + |
| 510 | +- **See also:** |
| 511 | + - [v-once](#v-once) |
456 | 512 |
|
457 | 513 | ## v-is <Badge text="deprecated" type="warning" />
|
458 | 514 |
|
|
0 commit comments