A problem occured when I set the content of a subcomponent to vnode
Version
3.1.1
Reproduction link
Steps to reproduce
- Call the
setVNode
method to set up thecontent
of the subcomponent in theonMounted
of the parent component. - Click the show target button to display the sub components
- It is found that setvnode has no effect
- Click the set vnode content button, and the set vnode effect will come out
- Click the hide target button to hide the subcomponent, and then click the show target button to show the subcomponent again
- Find that the
content
of the subcomponent becomes blank - The corresponding operation of the set html content button does not have this problem
What is expected?
The parent component can dynamically switch the content of the child component between HTML fragment and vnode
What is actually happening?
When the subcomponent is hidden, setting content to vnode is invalid; When the subcomponent is in the display state, setting the content to vnode is effective, but it becomes blank after it is hidden and displayed again
- Subcomponent can display content through content property or default slot settings. Content property supports HTML fragment strings and vnode
- Code supporting vnode
watch(() => props.content , (newVal, oldVal) => {
if(!internalInstance) return;
if (isVNode(newVal)) {
internalInstance.slots.default = () => [newVal];
innerContent.value = '';
} else {
if(isVNode(oldVal) && !isVNode(newVal)){
delete internalInstance.slots.default;
}
innerContent.value = newVal;
}
});
You cannot change the slots programatically, only declaratevely. Regarding your code, you can do this. BTW, the ref containing a vnode should be a shallowRef
Remember to use the forum or the Discord chat to ask questions!