简体   繁体   English

如何在Ember中使用组件中的组件

[英]How to use a component from within a component in Ember

G'day all, 都好

I'm trying to wrap a component with another to provide a simplified editing wrapper. 我试图将一个组件与另一个组件包装在一起,以提供简化的编辑包装器。

The component is to conditionally show either a label or a select component that allows the user to pick the right value. 该组件将有条件地显示允许用户选择正确值的标签或选择组件。

I want to wrap the power-select component, and pass it's values through to the sub-component, so the page template component reference looks like this: 我想包装power-select组件,并将其值传递给子组件,因此页面模板组件参考如下所示:

{{cm-select 
    title="Country ##" 
    options=countries 
    selected=selectedCountry 
    searchField="name" 
    action="selectCountry"
 }}

"countries" is an array of country objects, and selectedCountry is one of those country objects. “国家”是国家对象的数组,selectedCountry是这些国家对象之一。

The component template has the following: 组件模板具有以下内容:

<td>{{title}}</td>
 <td>
  {{#if edit}}
     {{#power-select
       options=options
       selected=selected
       searchField=searchField
        as |object|}}
        {{object.name}}
     {{/power-select}}
  {{else}}
      <small>{{modelElement}}</small>
  {{/if}}
</td>

Unfortunately the power-select component renders with an empty list of options. 不幸的是,电源选择组件呈现的选项列表为空。

I thought wrapping those parameters in handlebars might do the trick, but it seems that handlebars in handlebars isn't a valid syntax. 我以为将这些参数包装在车把中可以解决问题,但似乎车把中的车把不是有效的语法。

Does anyone have any ideas? 有人有什么想法吗?

Thanks, 谢谢,

Andy 安迪

That should work, I created a twiddle for you, demonstrating your use case. 那应该起作用,我为您创建了一个旋转控件 ,演示了您的用例。 You'll see I updated the your cm-select template to this: 您会看到我将cm-select模板更新为:

{{title}} |
<button {{action 'toggleEdit'}}>Toggle Edit</button>
<br/>
{{#if edit}}
    Search for a Item via {{searchField}}
  <br/>
  {{power-select
    options=options
    selected=selected
    searchField=searchField
    onSelect=(action "itemSelected")
  }}
{{else}}
  {{search-list 
    options=options 
    searchField=searchField
    onSelect=(action "itemSelected")
  }}
{{/if}}

Where you iterated over options for power-select in the cm-select component, I moved that down into the power-select template. 在迭代cm-select组件中用于power-select的选项的地方,我将其移至power-select模板中。 It's better to try and encapsulate some functionality there, instead of having everything in cm-select. 最好尝试在其中封装一些功能,而不是将所有功能都放在cm-select中。 I wasn't sure what you had in mind with {{modelElement}} so I just demonstrate what it would look like, using two different components in cm-select. 我不确定{{modelElement}}想法,所以我只演示了它的样子,在cm-select中使用了两个不同的组件。

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

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