简体   繁体   English

Ember“传递的动作为空或在(动作)中未定义”错误

[英]Ember “Action passed is null or undefined in (action)” error

I have an Ember component checkout-form that contains some logic for handling a checkout process.我有一个 Ember 组件checkout-form ,其中包含一些用于处理结帐流程的逻辑。 Here's a simplified version of how I'm using it:这是我如何使用它的简化版本:

{{#checkout-form}}

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

{{/checkout-form}}

Inside of my checkout-form.js component I have the following action:在我的checkout-form.js组件中,我有以下操作:

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

And finally inside of my course-date.js component I have:最后在我的course-date.js组件中,我有:

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

However, when running this code I get the error:但是,运行此代码时出现错误:

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

What am I missing here?我在这里缺少什么? I am passing the action into the course-date component and not sure why is it asking for a controller?我正在将操作传递到course-date组件中,但不确定它为什么要求控制器?

Reason for the error is,错误的原因是,
You are accessing selectDate which is undefined in that scope(ie., controller) If you do {{log 'selectDate value is ' selectDate}} inside that checkout-form which will print selectDate value is undefined .您正在访问在该范围(即控制器)中未定义的selectDate如果您在该结帐表单中执行{{log 'selectDate value is ' selectDate}} ,它将打印selectDate value is undefined so if you want to access any properties, actions which are defined in the component then that component should yield those values.因此,如果您想访问组件中定义的任何属性和操作,则该组件应产生这些值。

Here is twiddle which demonstrates how you can yield action from component.这是twiddle,它演示了如何从组件中产生动作。

application.hbs应用程序.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应用程序.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 - Here we are yielding selectDate action模板/组件/checkout-form.hbs - 这里我们产生selectDate动作

{{yield (action 'selectDate')}}

components/checkout-form.js组件/结帐-form.js

import Ember from 'ember';

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

course-date.hbs - Here we are just using the closure action that is passed to this component. course-date.hbs - 这里我们只是使用传递给这个组件的关闭操作。

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

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

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