If i have a component with a single slot and instead of just rendering all of it's children I want to wrap each element, I do something like this:
Vue.component('MyElement', {
render: function(createElement){
for (let i = 0; i < this.$slots.default.length; i++) {
//create a wrapper element
let wrappedElement = createElement("div", {}, this.$slots.default[i]);
// replace the current element in this slot, with the wrapped element
this.$slots.default[i] = wrappedElement;
}
return createElement("div", this.$slots.default);
}
}
Used like this:
<MyElement ref="myElement">
<p>Item 1</p>
<p>Item 2</p>
</MyElement>
Which ends up looking like this
<div>
<div>
<p>Item 1</p>
</div>
<div>
<p>Item 2</p>
</div>
</div>
Up to this point everything is great.
Now when I'd like to insert another <p>
element into <MyElement>
using
// get reference to <MyElement>
const myElement = this.$refs["myElement"];
// create a new element
var newElement = document.createElement("div");
newElement.innerText = "Hiya";
myElement .$el.appendChild(newElement);
The new element won't get wrapped, because render is not invoked again, how can I take full control of rendering for each child in my slot? or is there a better when to append children programatically into a component?
Thanks
If you could consider creating a
component
for the Items. So that when you want to add a new item, you could just create amethod
which will invoke the item component.I made a Code Snippet for this example.