Vue 3 v-memo Directive
v-memo The directive is used to memoize a template's subtree, deciding whether to skip updates by specifying a dependency value array.
Available only in Vue 3.2+.
Basic Explanation
v-memo The directive allows you to memoize part of a template. It expects a fixed-length dependency value array. If each value in the array is the same as during the last render, the update of the entire subtree will be skipped.
- Expected type: any[]
- Purpose: memoize the template subtree, deciding whether to skip updates by specifying a dependency value array.
Basic Syntax
Example
Never update
Use Cases
1. Long List Optimization
v-memo is especially useful when processing large lists, as it can skip list items that do not need updating:
Example
v-for="item in items"
:key="item.id"
v-memo="[item.isSelected, item.isExpanded]"
class="item"
>
{{ item.title }}
{{ item.description }}
Difference Between v-memo and v-once
| Instruction | Purpose | Applicable scenarios |
|---|---|---|
| v-once | Never update | Content is completely static and needs no updates |
| v-memo | No update when dependencies are unchanged | Content needs updating only when specific dependencies change |
Vue 3 Built-in Directives