Javascript 余烬“;(操作)中传递的操作为空或未定义;错误
我有一个Ember组件Javascript 余烬“;(操作)中传递的操作为空或未定义;错误,javascript,ember.js,Javascript,Ember.js,我有一个Ember组件签出表单,它包含一些处理签出过程的逻辑。下面是我如何使用它的简化版本: {{#checkout-form}} {{#each model.courses.otherDates as |date|}} {{course-date model=date selectDate=(action selectDate) }} {{/each}} {{/checkout-form}} 在我的签出表单.js组件中,我有以下操作: selectDate(day) {
签出表单
,它包含一些处理签出过程的逻辑。下面是我如何使用它的简化版本:
{{#checkout-form}}
{{#each model.courses.otherDates as |date|}}
{{course-date model=date selectDate=(action selectDate) }}
{{/each}}
{{/checkout-form}}
在我的签出表单.js
组件中,我有以下操作:
selectDate(day) {
this.set("startAt", day.get("serverString"))
}
最后,在我的课程日期.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>.
ember.debug.js:19818断言失败:在来自的(操作)中传递的操作为null或未定义。
我错过了什么?我正在将操作传递到
课程日期组件中,不确定它为什么要求控制器?错误原因是,
如果您在将打印selectDate值未定义的签出表单内执行{log'selectDate值为'selectDate}}
,则您正在访问该范围内未定义的selectDate
。因此,如果您想要访问组件中定义的任何属性和操作,那么该组件应该生成这些值
下面是如何从组件生成操作
<button {{action selectDate}}> CourseDate </button>
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')}}
组件/checkout-form.js
import Ember from 'ember';
export default Ember.Component.extend({
actions:{
selectDate(){
console.log(' selectDate in checkout-form component');
}
}
});
course-date.hbs-这里我们只使用传递给此组件的关闭操作
<button {{action selectDate}}> CourseDate </button>
CourseDate
{{yield(action'selectDate')}}
做得很好。非常感谢。