Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ember.js Ember,将动作从模板传递到组件时没有动作处理程序_Ember.js - Fatal编程技术网

Ember.js Ember,将动作从模板传递到组件时没有动作处理程序

Ember.js Ember,将动作从模板传递到组件时没有动作处理程序,ember.js,Ember.js,我试图将一个操作从路由传递到模板,然后再传递到组件 app/routes/application.js actions: { showModal(context) { console.log("This needs to be triggered" + context) }, } {{some-component showModal=showModal }} <button {{action "showModal" "The context f

我试图将一个操作从路由传递到模板,然后再传递到组件

app/routes/application.js

  actions: {
    showModal(context) {
      console.log("This needs to be triggered" + context)
    },
  }
{{some-component 
  showModal=showModal
}}
<button {{action "showModal" "The context for the action"}}>Press Me</button> 
<button {{action "showModal" "The context for the action"}}>Press Me</button>
app/templates/application.hbs

  actions: {
    showModal(context) {
      console.log("This needs to be triggered" + context)
    },
  }
{{some-component 
  showModal=showModal
}}
<button {{action "showModal" "The context for the action"}}>Press Me</button> 
<button {{action "showModal" "The context for the action"}}>Press Me</button>
app/components/some component/template.hbs

  actions: {
    showModal(context) {
      console.log("This needs to be triggered" + context)
    },
  }
{{some-component 
  showModal=showModal
}}
<button {{action "showModal" "The context for the action"}}>Press Me</button> 
<button {{action "showModal" "The context for the action"}}>Press Me</button>
按我
当运行这个时。我听到一个错误说

“没有:showModal的操作处理程序”

尽管如此,当我在模板/application.hbs中包含操作而不将其传递给组件时,一切正常。只是在将操作传递给组件时

app/templates/application.hbs

  actions: {
    showModal(context) {
      console.log("This needs to be triggered" + context)
    },
  }
{{some-component 
  showModal=showModal
}}
<button {{action "showModal" "The context for the action"}}>Press Me</button> 
<button {{action "showModal" "The context for the action"}}>Press Me</button>
按我

这很有效。不过,我想在组件中调用此操作。如何将此操作传递给组件?

触发路由操作与此上下文中来自控制器的触发操作稍有不同。当从控制器或其他组件向组件传递动作时,将其包装在动作辅助对象中,如下所示:

{{some-component showModal=(action "showModal")}}
由于您试图传入的操作在路由中存在,因此您需要利用控制器中的方法来调用路由中的操作。将其传递到组件中,如下所示:

{{some-component showModal=(action send "showModal")}}

下面是一个有助于将其组合在一起的示例。

在这种情况下,触发路由操作与来自控制器的触发操作略有不同。当从控制器或其他组件向组件传递动作时,将其包装在动作辅助对象中,如下所示:

{{some-component showModal=(action "showModal")}}
由于您试图传入的操作在路由中存在,因此您需要利用控制器中的方法来调用路由中的操作。将其传递到组件中,如下所示:

{{some-component showModal=(action send "showModal")}}

这里有一个方法可以帮助您将其拼凑起来。

您也可以使用它。但总的来说,我不建议采取这样的行动。这是核心团队不推荐的模式。“请看一下关于这个问题的讨论。”史蒂夫尼尔伯格回答得非常详细,这个小动作帮了大忙。谢谢你。你也可以用。但总的来说,我不建议采取这样的行动。这是核心团队不推荐的模式。“请看一下关于这个问题的讨论。”史蒂夫尼尔伯格回答得非常详细,这个小动作帮了大忙。非常感谢。