Ember "Action passed is null or undefined in (action)" 错误

Ember "Action passed is null or undefined in (action)" error

我有一个 Ember 组件 checkout-form,其中包含一些用于处理结帐流程的逻辑。这是我如何使用它的简化版本:

{{#checkout-form}}

  {{#each model.courses.otherDates as |date|}}
    {{course-date model=date selectDate=(action selectDate) }}
  {{/each}}

{{/checkout-form}}

在我的 checkout-form.js 组件中,我有以下操作:

selectDate(day) {
  this.set("startAt", day.get("serverString"))
}

最后在我的 course-date.js 组件中我有:

click () {
  const courseStart = this.get('courseStart')
  this.get('selectDate')(courseStart)
}

但是,当 运行 这段代码时,我得到错误:

ember.debug.js:19818 Assertion Failed: Action passed is null or undefined in (action) from <site@controller:checkout/date::ember389>.

我在这里错过了什么?我正在将操作传递给 course-date 组件,但不确定为什么要请求控制器?

错误原因是,
您正在访问该范围内未定义的 selectDate(即控制器)如果您在该结帐表单中执行 {{log 'selectDate value is ' selectDate}},它将打印 selectDate value is undefined。因此,如果您想访问组件中定义的任何属性、操作,那么该组件应该产生这些值。

这里是twiddle which demonstrates如何从组件产生动作。

application.hbs

{{#checkout-form as |selectDate| }}
 {{!-- 
 here context is controller not the checkout-form component
 Whatever you want to access from component, then component should yield those values.
 --}}
 {{course-date selectDate=(action 'selectDateInController')}}
 {{course-date selectDate=selectDate}}
{{/checkout-form}}

application.js

import Ember from 'ember';
export default Ember.Controller.extend({
  appName: 'Ember Twiddle',
  actions:{
    selectDateInController(){
      console.log(' selectDate in controller');
    }
  }
});

templates/components/checkout-form.hbs - 这里我们产生 selectDate 动作

{{yield (action 'selectDate')}}

components/checkout-form.js

import Ember from 'ember';

export default Ember.Component.extend({
   actions:{
    selectDate(){
      console.log(' selectDate in checkout-form component');
    }
  }
});

课程-date.hbs - 这里我们只是使用传递给这个组件的关闭动作。

<button {{action selectDate}}> CourseDate </button>