Skip to content
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

Passing component to grandchildren's nested named slot will resolve in the default slot. #6996

Closed
DaxChen opened this issue Nov 3, 2017 · 5 comments
Labels

Comments

@DaxChen
Copy link

DaxChen commented Nov 3, 2017

Version

2.5.2

Reproduction link

https://jsfiddle.net/daxchen/k75y6tu0/

Steps to reproduce

My use case is that I have a default layout with Toolbar and main content wrapper. But some pages needs a SearchBar and some don't, and layouts may have different levels.

So I created a component called LayoutDefault as a base for other layouts to use. And because I want to be able to replace the default Toolbar when needed, I put the default Toolbar in <slot name="toolbar">, with Toolbar as fallback content.

Code snippet:

// LayoutDefault.vue
<slot name="toolbar">
  <Toolbar title="title from layout-inner">
    <slot name="toolbarSlot"></slot>
  </Toolbar>
</slot>

I make these kind of components because I want to be able to:

  1. Replace the default Toolbar if need.
  2. Using the default Toolbar, but pass things to its slot ("toolbarSlot" in above example)

And then in other layout components, I do this:

// LayoutLv1.vue
<div class="layout">
  <LayoutDefault>
    <slot name="toolbar" slot="toolbar"></slot>
    <slot name="toolbarSlot" slot="toolbarSlot"></slot>
    <slot>default content from Layout</slot>
  </LayoutDefault>
</div>

As you can see, to pass components to grandchild's slots, I used this pattern: <slot name="foo" slot="foo">.

For some reason, components passed using this pattern will be passed to the default slot if there's one.

See examples 2, 3, and 4 in the reproduction.

What is expected?

I expect when no nothing is passed to a named slot, the fallback content will be rendered, with it's slots too.

What is actually happening?

In example 2, the replaced Toolbar is rendered in LayoutDefault's default slot.

In example 3 and 4, SearchBar is passed to slot="toolbarSlot", but not rendered.


Is this the expected behavior?
Or if there's a way for me to make these kind of reusable layouts with each default parts replaceable, and if using the default parts, be able to pass components into their (default parts) slots?

@DaxChen
Copy link
Author

DaxChen commented Nov 3, 2017

WOW that's amazing!
Cloned, built, and tested, it works!!
Evan you're the best~~~
Thank you so much!!!

@yyx990803
Copy link
Member

btw this is already out in 2.5.3 :)

@faragos
Copy link

faragos commented Jan 8, 2018

Does this also work with slot-scope in 2.5.3? :)

lovelope pushed a commit to lovelope/vue that referenced this issue Feb 1, 2018
@alex-martinez
Copy link

@faragos here's how I accomplished passing slots to a grandchild that uses slot-scope.

https://jsfiddle.net/alexm/5hx43mb2/

@rcosta-gcare
Copy link

@alex-martinez thanks for posting your solution, it was a big help :)

f2009 pushed a commit to f2009/vue that referenced this issue Jan 25, 2019
aJean pushed a commit to aJean/vue that referenced this issue Aug 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants