Guide: List Rendering Flashcards
What is the syntax of v-for
?
item in items
where items
is the source data array, and item
is an alias for the array element being iterated on
You can also use item of items
How can access the index in a v-for
?
v-for="(item, index) in items"
What are the three syntaxes for iterating over an object?
- Iterating through the values of an object’s properties
v-for="value in object"
- Iterating through the values and keys of an object’s properties
v-for="(value, key) in object"
- Iterating through the values, keys, and the index
v-for="(value, key, index) in object"
How is order determined when looping through objects?
It’s determined using the builtin Object.keys()
, which is not guaranteed to be consisten across JS engines
What is the default strategy Vue uses when updating a list rendered with v-for
, and when is it suitable?
“in-place patch”
Only suitable when…
- Your list render output does not rely on child component state
- Your list render output does not rely on temporary DOM state (such as an input filter box)
- The content is simple
How do you enable Vue to track each node’s identity, and what will this allow Vue to do?
Use a unique value in the key
attribute.
Allows Vue to track each node’s identity, and thus reuse and reorder existing elements
`
`
What does Vue do to observed arrays? Give detail.
Vue wraps observed arrays’ mutation methods so that they’ll trigger view updates
The wrapped methods are:
push()
pop()
shift()
unshift()
splice()
sort()
reverse()
Describe how replacing an array works with non-mutation methods.
Some methods, like filter()
, do not mutate the array, and return a new one.
If you assign the filtered array to the existing one, it will be a very efficient operation since Vue maximizes DOM element reuse when replacing an array with another array containing overlapping objects.
What are the detection caveats in relation to arrays? And how we do overcome them?
- Vue cannot detect directly setting an item with an index
vm.items[idx] = newValue
-
Two options to overcome this:
vm.$set(vm.items, idx, newValue)
vm.items.splice(idx, 1, newValue)
- Vue cannot detect when you modify the length of the array
vm.items.length = newLength
-
Use this to deal with this caveat
vm.items.splice(newLength)
What are the object change detection caveats?
- Vue cannot detect object property addition or deletion
- Can overcome this by using
vm.$set
to add reactive properties- `vm.$set(vm.userProfile, ‘age’, 32)
- Can overcome this by using
- To assign multiple new properties to an existing object:
- Create a new object with properties from both, using
Object.assign()
or_.extend()
and then assign to fresh objectvm.userProfile = Object.assign({}, vm.userProfile, {...})
- Create a new object with properties from both, using
How should we handle displaying filtered or sorted results of an array?
Use a computed property that returns the filtered or sorted array.
In situations where computed properties are not feasible, like in a v-for
loop, use a method.
- Understand that using any instance data in the computed property will not trigger the property to update in the view because it’s not actually a bound property. The method was simply invoked for each render.
How can we iterate over a range in the template?
Use v-for
with an integer. It will repeat the template that many times.
{{ n }}
How do you render a block of multiple elements a certain number of times?
Use v-for
on an encapsulating template
tag
Describe v-for
and v-if
on the same node.
When existing on the same node, v-for
has a higher priority than v-if
, thus, v-if
will be run on each iteration of the loop separately.
If you conditionally want to skip the loop, place v-if
on a wrapper element, or template
tag
Describe using v-for
with a component.
-
v-for
can be used directly on a custom component, like any other element - the
key
attribute is required for custom components - Using
v-for
on a custom component does not automatically pass any datat to the component.- You must still explicity pass your props in
*
- You must still explicity pass your props in