简体   繁体   English

如何访问svelte组件中的命名插槽?

[英]How to access named slots inside svelte component?

I'm trying out Svelte (great!) but I'm running into a problem that I don't know how to solve. 我正在尝试Svelte(太棒了!),但是遇到了一个我不知道如何解决的问题。 I have a component with a couple of named slots. 我有一个带有几个命名插槽的组件。 Based on whether these slots are filled, I need to render some additional HTML. 根据是否填充了这些插槽,我需要呈现一些其他HTML。 So my idea was to put these blocks inside an {{#if slots}} block, but I don't know how to refer to the named slots. 所以我的想法是将这些块放在{{#if slots}}块中,但是我不知道如何引用命名的槽。 Trying this.options.slots in oncreate , I can see the collection of slots, but I don't know how to get to them in the HTML part of my component. oncreate尝试this.options.slots ,我可以看到插槽的集合,但是我不知道如何在组件的HTML部分中找到它们。 Anyone able to help me out? 有人可以帮助我吗? See this REPL 看到这个REPL

Elco already figured out the answer and mentioned it in a comment, but for anyone else who comes across this — it's a little hacky, but you can do this.set(...) in the oncreate hook: Elco已经找到答案,并在评论中提到了它,但对于遇到此问题的其他人来说,这有点this.set(...) ,但是您可以在oncreate挂钩中执行this.set(...)

oncreate () {
  this.set({
    hasEmail: !!this.options.slots.email
  });
}

Demo here . 演示在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM