简体   繁体   English

如何继承带有苗条组件的插槽?

[英]How to inherit slots with svelte components?

There is a component <Bar> that inherits from a component <Foo> .有一个组件<Bar>继承自组件<Foo>

The <Foo> component defines a slot named content with a fallback content. <Foo>组件定义了一个名为content的插槽,其中包含一个备用内容。 The <Bar> component also defines a slot named content with a different fallback content. <Bar>组件还定义了一个名为content的插槽,其中包含不同的备用内容。

<!-- Foo.svelte -->
<slot name="content">
  foo fallback
</slot> 

<!-- Bar.svelte -->
<script>import Foo from './Foo.svelte'</script>
<Foo>
    <span slot="content">
        bar fallback
    </span>
</Foo>

The <Bar> component displays the fallback when called with <Foo/> but it does not display the custom content when called with <Bar><span slot="content">bar custom</span></Bar> . <Bar>组件在使用<Foo/>调用时显示回退,但在使用<Bar><span slot="content">bar custom</span></Bar>调用时不显示自定义内容。

Can you tell me what I am doing wrong?你能告诉我我做错了什么吗?

<Foo/>
<!-- prints 'foo fallback': OK -->

<Foo>
    <span slot="content">foo custom</span>
</Foo>
<!-- prints 'foo custom': OK -->

<Bar/>
<!-- prints 'bar fallback': OK -->

<Bar>
    <span slot="content">bar custom</span>
</Bar>
<!-- prints 'bar fallback': KO - I would have expected 'bar custom' -->

Note that this code is oversimplified, and my real-world usecase is a bit more complex than this, and I need to use slots there.请注意,此代码过于简单,我的实际用例比这要复杂一些,我需要在那里使用插槽。

The fiddle: https://svelte.dev/repl/5c525651eb8b4f60a6a696c1bd19f723小提琴: https://svelte.dev/repl/5c525651eb8b4f60a6a696c1bd19f723

It looks like you want to render slot content passed to Bar in the slot defined by Foo , but currently you haven't defined a slot in Bar so there is nowhere for the passed content to be used, thus the fallback value is always passed to Foo .看起来您想在Foo定义的插槽中呈现传递给Bar的插槽内容,但目前您尚未在Bar中定义插槽,因此无处可使用传递的内容,因此后备值始终传递给Foo

A solution might look something like the following, defining a content slot in Bar inside the span passed to Foo 's content slot.解决方案可能如下所示,在传递给Foo的内容槽的跨度Bar中定义一个content槽。

// App.svelte

<script>
    import Foo from './Foo.svelte'
    import Bar from './Bar.svelte'
</script>

Expected: <em>bar custom</em><br>
<Bar>
    <span slot="content">bar custom</span>
</Bar>
// ./Bar.svelte

<script>
  import Foo from './Foo.svelte';
</script>

<Foo>
  <span slot="content">
    <slot name="content">bar fallback</slot>
  </span>
</Foo>
// ./Foo.svelte

<slot name="content">
  foo fallback
</slot> 

REPL REPL

I think most of the answers were pretty close.我认为大多数答案都非常接近。 I believe you can fix it by creating a slot with both content and name being the same value.我相信您可以通过创建一个内容和名称都具有相同值的插槽来修复它。 Building off of your example ( Repl with full 3rd level inheritance ),以您的示例为基础( Repl 具有完整的第 3 级 inheritance ),

<!-- Foo.svelte -->
<slot name="content">
  foo fallback
</slot> 
<!-- Bar.svelte -->
<script>import Foo from './Foo.svelte'</script>
<Foo>
  <slot name="content" slot="content">
    bar fallback
  </slot>
</Foo>

By doing it this way, we are both passing the slot through and including our new fallback.通过这种方式,我们既可以通过插槽,也可以包含我们的新后备。

If you were looking to make sure the span is there, that can be done by either putting it inside or outside of the slot depending on your use case.如果您想确保跨度在那里,可以根据您的用例将其放在slot内部或外部来完成。

<!-- Bar.svelte -->
<script>import Foo from './Foo.svelte'</script>
<!-- option 1-->
<Foo>
  <span>
    <slot name="content" slot="content">
      bar fallback
    </slot>
  </span>
</Foo>
<!-- option 2-->
<Foo>
  <slot name="content" slot="content">
    <span>
      bar fallback
    </span>
  </slot>
</Foo>

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

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