Ember在组件中产生多个动作

时间:2017-07-26 08:00:00

标签: javascript ember.js

我有一个checkout-form组件,其中包含nextprevioussubmitFormselectDate等操作。目前,我只能yield selectDate这样的行动:

{{!-- checkout-form.js --}}
<div class='checkout-form'>
  {{yield (action 'selectDate')}}
</div>

我希望能够像我这样使用我的checkout-form组件:

{{!-- order.hbs --}}
{{#checkout-form as |submitForm, selectDate|}} 
  {{checkout-field placeholder="Full Name" value=model.order.name}}
  {{!-- another field component that uses selectDate --}}
  {{checkout-form-actions action=submitForm}}
{{/checkout-form}}

如何在我的checkout-form.hbs内部使用多个动作?

1 个答案:

答案 0 :(得分:4)

选项1。您可以传递多个参数,如下所示

{{yield (action 'selectDate') (action 'submitForm')}}

阅读 - https://guides.emberjs.com/v2.14.0/components/block-params/

{{!-- order.hbs --}}
{{#checkout-form as |selectDate, submitForm|}} 
  {{checkout-form-actions action=selectDate}}
  {{checkout-form-actions action=submitForm}}
{{/checkout-form}}

选项2。您还可以使用hash helper

{{yield (hash 
        selectDate=(action 'selectDate')
        submitForm=(action 'submitForm')) }}

{{#checkout-form as |options|}}   
  {{checkout-form-actions action=options.submitForm}}
{{/checkout-form}}