-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
Create a v-if-parent template directive #10078
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
It looks to me that you want to reuse parts of the template. This is a duplicate of #5401 For the moment, to reuse fragments of a template, you need to either create subcomponents or use JSX/render functions |
The duplicate you provided would only be a bad workaround for the issue and will most likely not be implemented until some years I'm asking for a different v-if which instead of not rendering the component and all it's children would not render the component but still render it's children. Please reopen this issue |
In that case, that should be implemented in a custom component, in user lad. It's not possible to render the children in every scenario as there could be multiple of them |
If some people stumble here, this is the component I ended up doing export default {
name: "v-if-parent",
props: {
condition: {
type: Boolean,
required: true
}
},
render(el) {
if (this.condition) {
let parent = this.$scopedSlots.parent()[0];
parent.children = this.$scopedSlots.default();
return parent;
} else {
let children = this.$scopedSlots.default();
if (children.length > 1) {
//We can only return a single vnode so if multiple children, wrap them in a div
return el('div', null, children)
}
return children;
}
}
} And then use it like this <v-if-parent :condition="showparent">
<template #parent><a href="somelink" /></template>
This text is always visible <span>You can include absolutely whatever you want in the default slot</span>
</v-if-parent> |
What problem does this feature solve?
I would like to have an optional component but with the content being always rendered (so v-if doesn't work with this use case)
It's currently not possible without a lot of code duplication
What does the proposed API look like?
What I would like to be able to write instead is this
The text was updated successfully, but these errors were encountered: