vuejs / vue

This is the repo for Vue 2. For Vue 3, go to https://github.com/vuejs/core

Home Page:http://v2.vuejs.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Vue.compile(template) - Direct siblings from same component on template change have wrong reference in life-cycle hooks

shadowtsw opened this issue · comments

commented

Version

2.7.10

Reproduction link

github.com

Steps to reproduce

We use Vue.compile() to render dynamic content at runtime. All components make use of life-cycle hooks e.g. to [provide] attributes to deeply nested slot components.

Create two template strings with wrapper (e.g. div id=xy), inside two or three vue-components from same vue file. In the second template, remove the first component. At runtime, use Vue.compile to reder content, switch the templates and e.g. log attributes in life-cycle hooks.

What is expected?

The log has to show the attributes from first component (which one has been removed).

What is actually happening?

The log shows the attributes from last component of all siblings.
Watchers on attributes, trigger and show the old value (attr from last sibling) and new value (current and correct attribute from related component).


Tried many things, but nothing works. My colleague has a small reproducable example which shows the problem.

Could you explain further what you mean by this

"Create two template strings with wrapper (e.g. div id=xy), inside two or three vue-components from same vue file. In the second template, remove the first component. At runtime, use Vue.compile to reder content, switch the templates and e.g. log attributes in life-cycle hooks."

Not sure how this is shown in the given github repo - could you please provide a more accurate code recreation?

commented

Sure, my colleague has updated the example, also with explanation. Hopefully, the problem becomes understandable.

commented

In addition, if same type siblings are seperated by another type of component (try out to insert a <br> between), everything works as expected.

commented

Solved....
It seems the template compiler also needs the "key" property on custom components to refer to the correct instance if template changes (add or remove components).