簡體   English   中英

在 ember 中生成完整的塊

[英]Yield complete block in ember

我想知道如何生成傳遞給組件的完整塊。 我已經找到了這個https://guides.emberjs.com/v2.9.0/components/block-params/但我不明白為什么有

//my-component.hbs
{{#if hasBlock}}
  {{yield post.title}}
  {{yield post.body}}
  {{yield post.author}} ...

為什么我必須命名我想要產生的東西? 這是沒有意義的,因為我想產生(顯示)我傳遞給組件的整個塊,不管我在那里做什么。

所以我嘗試只使用 yield :

//my-component.hbs
{{#if hasBlock}}
  {{yield}} ...

並以這種方式使用組件:

//myroute.hbs
{{#my-component car=model}}
  {{car.name}} - {{car.color}}
{{/my-component}}

這不起作用,但我希望'car.name - car.color' 將在組件的 {{yield}} 中呈現...

有人可以向我解釋一下嗎?

這有點令人困惑,我承認。 發生的事情是組件正在產生(返回)您可以在塊中使用的值。 嘗試這樣的事情:

{{#my-component car=model as |car|}}
    {{car.name}} - {{car.color}}
{{/my-component}}

然后在您的組件中:

{{#if hasBlock}}
    {{yield car}}
{{else}}
    default no-block template goes here...
{{/if}}

這是一個有效的技巧: https ://ember-twiddle.com/9a0efb92ebea6f206236a32e3c3e6053 ? openFiles = templates.index.hbs%2Ctemplates.components.my-component.hbs

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM