core: Incorrect template ref
Version
3.0.4
Reproduction link
https://codesandbox.io/s/misty-tree-xzmh1?file=/src/main.js
Steps to reproduce
Open the link, check the console
What is expected?
The template ref should be <div>hello</div>
What is actually happening?
The template ref is <div>foo</div>
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 20 (14 by maintainers)
Commits related to this issue
- fix(runtime-core): incorrect key-less node resuing cause incorrect template ref (#2790) — committed to luwuer/vue-next by luwuer 4 years ago
Is this because the
<div>is being reused across renderings and the console logging shows the current content of that<div>, not the content it had when it was logged? The content of that<div>gets updated when it re-renders. You’d need to give it akeyfor the diffing to pair up the correct nodes.Using
console.log(refDom.value.innerHTML);seems to give the correct value.I think this is a bug, if you replace patchKeyedChildren with
patchUnkeyedChildren, then everything will work fine.Check this link, the click event will be bound to
div(foo): https://codesandbox.io/s/throbbing-snowflake-vpm1x, but when mounted, patch has not been performed yet, let alone reuse, thediv(foo)doesn’t exist yet, right?In this reproduction, if you remove the
refVal.value && h('span', 'bar')sentence, it will also work correctly, I tracked this issue and its update process is as follows:The pre-vnode:
The next-vnode:
prev-comment1prev-div(hello) ---> next-div(foo)comment2next div(bar)next div(hello)But I don’t know what caused the problem, it’s weird, just like @edison1105 said.
Your solution is not correct. There is no problem in reusing nodes, because
pre-div(hello)andnext-div(foo)should be reusable, because their type(div) and key(undefined) are the same, which has nothing to do withrefdom.