I have a responsive-menu component which I want to use named slots inside of this up my template markup:
我有一个响应菜单组件,我想在我的模板标记中使用命名槽:
<template>
<div class="responsive-menu">
<div class="menu-header">
<slot name="header"></slot>
</div>
</div>
</template>
Whenever I try my named slot like this it work perfectly fine:
每当我像这样尝试我的命名槽时,它都工作得很好:
<responsive-menu>
<h3 slot="header">Responsive menu header</h3>
</responsive-menu>
However as soon as I wrap it with a class nothing shows up anymore.
然而,一旦我用一个类来包装它,就什么都不会出现了。
<responsive-menu>
<div class="container">
<h3 slot="header">Responsive menu header</h3>
</div>
</responsive-menu>
What is going on here? Shouldn't I just be able to wrap the named component? Which does it appear that my named slots need to be direct children of my Vue component?
这里发生什么事情?难道我不应该只包装命名组件吗?我的命名插槽似乎需要是我的Vue组件的直接子组件?
更多回答
Umm.. I reproduced your case and it works?
嗯..。我复制了你的案子,它起作用了?
@AmreshVenugopal You are using a header slot, yet your html does not have any reference to this slot. Meaning it will simply fall back to it's default slot content.
@AmreshVenugopal您正在使用标题栏,但您的html没有任何对此栏的引用。这意味着它将简单地后退到其默认的槽内容。
By the way I am using webpack with .vue
components I will try if I can get a webpackbin file up and running.
顺便说一句,我正在使用带有.VUE组件的webpack,如果我能启动并运行一个WebPackbin文件,我会试一试。
The only thing I can think is OP is actually doing <responsive-menu><another-component><slot>...</slot></another-component></responsive-menu>
. If that's not the case this works just fine.
我唯一能想到的就是OP实际上是在做....如果不是这样的话,这种方法运行得很好。
I hadn't registered the component, now I see it blanked out.
我还没有注册组件,现在我看到它被删除了。
优秀答案推荐
It does not work because your "wrapped slot" isn't direct child of responsive-menu tag.
它不起作用,因为您的“包装槽”不是响应菜单标签的直接子标签。
try something like that:
试着这样做:
<responsive-menu>
<div class="container" slot="header">
<h3>Responsive menu header</h3>
</div>
</responsive-menu>
jsfiddle
Jsfiddle
It works with Vue >= 2.6. Just upgrade vue and vue-template-compiler.
它适用于VUE>=2.6。只需升级VUE和VUE模板编译器即可。
You could use it like v-slot
because slot
syntax is deprecated as mentioned here:
您可以像使用v-槽一样使用它,因为槽语法已弃用,如下所述:
<responsive-menu>
<div class="container">
<h3 v-slot:header>Responsive menu header</h3>
</div>
</responsive-menu>
or Alternatively, a shorthand:
或者,一种速记:
<responsive-menu>
<div class="container">
<h3 #header>Responsive menu header</h3>
</div>
</responsive-menu>
更多回答
The slot doesn't need to be a direct child.
插槽不必是直接子对象。
I have tried it out and it does not need to be a direct child: webpackbin.com/bins/-KhXJnhf2meOIC9h5XS0 There is a problem with this example though because all the markup in my template surrounding the named slot is completely gone. And in my current codebase I still can't get it working properly.
我已经尝试过了,它不需要是直接的子级:webpack bin.com/bins/-KhXJnhf2meOIC9h5XS0这个示例有一个问题,因为我的模板中围绕命名槽的所有标记都完全消失了。在我目前的代码库中,我仍然无法让它正常工作。
If slot isn't direct child, slot attribute is ignored and ENTIRE content of component tag is used as content for default slot in component template markup.
如果插槽不是直接子项,则忽略插槽属性,并将组件标签的整个内容用作组件模板标记中默认插槽的内容。
@Stephan-v This fiddle should prove the answer.
@Stephan-v这把小提琴应该证明了答案。
@AmreshVenugopal So how would you make that particular fiddle working properly? Neverind I tried to remove the container and everything works than. I am really wondering if this is a bug or if this is meant to happen though.
@AmreshVenugopal,那么你如何让这把小提琴正常工作呢?不管怎么说,我试着把容器移开,但一切都很正常。我真的很想知道这是一个错误,还是这是注定要发生的。
我是一名优秀的程序员,十分优秀!