v-for children still rendering even when v-for source contain no items.
Version
3.1.1
Reproduction link
Steps to reproduce
- Create a v-for structure against an array
- Create a child element within this v-for
- You will find that I can render normally
- Delete certain item from the v-for source
For demonstration in playground: You will find that the page has been rendered successfully at first. Press "Clear" to see error message.
What is expected?
Suppose the specific child will no longer exist and will not render
What is actually happening?
The child still exists and therefore the handler in :ref throw exception
When I clicked "delete foo", suppose "foo" is no longer exists in vm, how come v-for children are still rendering?
I think this is a duplicated of https://github.com/vuejs/vue-next/issues/3557
I think this is a duplicate of #3557
Sorry I don't understand, so the key is that I need to check for el
is null
or undefined
at first before assigning el
into variable? Or I cannot use object instead of array to store el
?
you need to check that vm[k] exists before you try to assign to its member .el
<label :ref="el => vm[k] ? vm[k].el = el : null">{{ vm[k].bar }}</label>
as you can see the ref method can be called in various states, including where the element is removed and the call is with a null.
You need to check el
: https://v3.vuejs.org/guide/composition-api-template-refs.html#usage-inside-v-for