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

How to access named slots inside svelte component?

我正在试用 Svelte(太棒了!)但我 运行 遇到了一个我不知道如何解决的问题。我有一个带有几个命名插槽的组件。根据这些插槽是否已填充,我需要渲染一些额外的 HTML。所以我的想法是将这些块放在 {{#if slots}} 块中,但我不知道如何引用命名的插槽。在 oncreate 中尝试 this.options.slots,我可以看到槽的集合,但我不知道如何在组件的 HTML 部分找到它们。任何人都可以帮助我吗? See this REPL

Elco 已经找到答案并在评论中提到了它,但对于遇到此问题的任何其他人来说 — 这有点老套,但您可以在 oncreate 挂钩中执行 this.set(...)

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

Demo here.