[在Ember.js中,我正在设计一个上下文组件,并希望处理不存在子组件来显示一些常规信息的情况。
但是我无法弄清楚如何检测模板中是否存在标签。使用yeild和hash,如果标签不存在,则不会显示任何内容,仅此而已...
第一种情况:所有子组件均存在
<ParentComponent as |parent|>
<parent.mandatoryChild>
Something
</parent.mandatoryChild>
<parent.child>
Something else
</parent.child>
</ParentComponent>
第二种情况:parent.child
组件不存在
<ParentComponent as |parent|>
<parent.mandatoryChild>
Something
</parent.mandatoryChild>
{{! no parent.child here }}
</ParentComponent>
我的父组件模板是:
<div class="mandatoryChild-wrapper-class">
{{yield
(hash
mandatoryChild=(component this.mandatoryChildComponent)
)
}}
</div>
<div class="mandatoryChild-wrapper-class">
<!-- how to show some generic information when child is not present in the callee template? -->
{{yield
(hash
child=(component this.childComponent)
)
}}
</div>
<div class="mandatoryChild-wrapper-class">
{{yield (hash mandatoryChild=(component this.mandatoryChildComponent))}}
</div>
<div class="mandatoryChild-wrapper-class">
{{#if this.childComponent}}
{{yield (hash mandatoryChild=(component this.childComponent))}}
{{else}}
generic information
{{/if}}
</div>