-
-
Notifications
You must be signed in to change notification settings - Fork 33.6k
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
Can not clear slot content with vue@2.5.3 #7064
Comments
IMO 2.4.4 is behaving incorrectly and 2.5.3 fixes the behavior. By design, fallback content appears when a slot receives nothing or all whitespace. "Clearing a slot's fallback content with an empty However if you still want to do that, you can use the zero-width space ( |
Using zero-width space to express “clearing the fallback content of a slot” sounds like a hack to me either...is there a better way for us to solve this with more explicit semantics? |
You can always add a prop "useDefaultSlot"(defaults to true) and use it
like
<slot><div v-if="useDefaultSlot">default content</div></slot>
…On Thu, Nov 16, 2017, 11:58 GU Yiling ***@***.***> wrote:
Using zero-width space to express “clearing the fallback content of a
slot” sounds like a hack to me either...is there a better way for us to
solve this with more explicit semantics?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#7064 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AFTLl7p9MpfDOiLP2UdCn5UuRih6ZgZOks5s27LTgaJpZM4QewVf>
.
|
Consider you have a complex component with multiple slots, you wouldn't want to expose that kind of prop to each of them. Using slots means we prefer a declarative way to define content, otherwise we can just ignore the slot syntax and pass render functions through |
@fnlctrl |
@Justineo For complex component with multiple slots, even if we provided an official api to suppress default content, users would still have to declare which slots to be suppressed. <foo :defaults="{slotA: false, slotB: false}"/> And I don't think it's any better than declaring props in userland. The best way I can think of is to simply use an empty <foo>
<p slot="a"/>
<p slot="b"/>
</foo> It's simple and declarative. |
Actually I'm not seeing why the behavior back to 2.4.4 is undesired. By declaring
If overriding slot fallback content is a common scenario, a built-in syntax will definitely help users to build apps with lower overhead on designing this kind of API. And this helps Vue users to understand what is the underlying logic of the template across different Vue projects. |
I think the "fallback content" is different with "default content", if we have defined fallback content, any behavior to make the content "empty" should be considered as a hack at first. From this point I agree with @yyx990803 . So here hack with a zero-width space is just good to me. Or you can define a "conditional" fallback content as @fnlctrl provided to set the content empty. Or you never define a fallback content as the default content. |
For a more consistent solution, I'd create an empty functional (stateless for better performance) component with a <template functional>
<span></span>
</template>
<script>
export default { name: 'VEmpty' }
</script>
// Usage
<template>
<v-foo>
<v-empty></v-empty>
</v-foo>
</template> 2.4.4 did seem more natural to me. I recently upgraded to 2.5.x and ended up here. :) |
|
A bare |
Yes you remind me of the old days that I have to support IE6...But what I want is a clean solution that does not prevent developers from leveraging modern technologies such as pseudo classes. |
Well, it's opinionated. With a year of developing a library, I didn't find much use for them or sense their absence. Not using |
@yyx990803 e.g. if you have a default "separator"-slot. Sometimes the iterated components may not have any html-tags in between because they break the float css styles. Also it's not clean to just use "empty" tags if it would be more logical to use an empty template. So if an exists, it should override the default value. |
So far, the best I've come up with is to use an empty separate component with a template: |
@yyx990803 It would be far easier to just pass zero content. It seems the most logical to the most amount of people, and adds zero hacks. The bug seems to be the fix from 2.4.4 to 2.5.3 Blank content is exactly what I want within the slot. |
@WORMSS Maybe you'll be interested in this: https://github.com/Justineo/vue-void |
Here is another solution:
|
It's not much better but you can also do <template #header><div v-show="false"/></template> The above will break :first-child CSS selectors if you use them in your slot
Justineo's solution above is really cool. You may be scared since it's a separate npm package - but it's as simple as: export default {
name: 'App',
components: {
HelloWorld,
VVoid: {
name: 'v-void',
render(h) {
return h();
}
}
},
} You can also register it globally for convenience. |
Wont that still add an unwanted div. Just because it's display: none can still potentially break CSS |
Yes it'll break :first-child selectors (the thing v-void's readme warns about). I edited my comment. The only benefit is that it uses simple template syntax |
Although it's a hack, too, the following code seems to do a trick without introducing any extra HTML: <template>
{{null}}
</template> |
Version
2.5.3
Reproduction link
https://jsfiddle.net/yibuyisheng/s5xoxmv1/6/
Steps to reproduce
Create a
Base
Component with a slot, and pass in the slot with template tag with empty content.What is expected?
The empty template slot should clear the content of
Base
Component's corresponding slot.What is actually happening?
Did not clear the content.
However, the vue@2.4.4 do the right thing: https://jsfiddle.net/yibuyisheng/s5xoxmv1/7/
The text was updated successfully, but these errors were encountered: