简体   繁体   English

Ember.js 组件操作未触发

[英]Ember.js component actions not fired

I am trying to make a reusable ember component.我正在尝试制作一个可重复使用的 ember 组件。 I want to add an action to this component, so when the user clicks a button somethings happen.我想向这个组件添加一个动作,所以当用户点击一个按钮时会发生一些事情。 The problem is, that this action is not been called.问题是,这个动作没有被调用。 I am doing it this way:我是这样做的:

DummyComponent.js虚拟组件.js

App.DummyComponent = Ember.Component.extend({
  actions: {
    dummy_action: function() {
      console.log('dummy_action fired!');
    } 
  } 
});

dummy-component.hbs虚拟组件.hbs

<p>dummy component</p>
<button {{action "dummy_action"}}>Dummy action</button>

The component is rendering correctly when I include in my application.hbs :当我包含在我的application.hbs 中时,组件正确呈现:

{{ dummy-component }}

But as I said, the action is not beeing fired.但正如我所说,行动并没有被解雇。

If I create an action in my controller using the same approach, the action is called correctly.如果我使用相同的方法在我的控制器中创建一个动作,该动作将被正确调用。

What am I doing wrong?我究竟做错了什么?

Thans!比!

From the component guide :组件指南

Note: Components must have a dash in their name.注意:组件的名称中必须有一个破折号。 So blog-post is an acceptable name, but post is not.所以 blog-post 是一个可以接受的名字,但 post 不是。 This prevents clashes with current or future HTML element names, and ensures Ember picks up the components automatically.这可以防止与当前或未来的 HTML 元素名称发生冲突,并确保 Ember 自动选择组件。

An example is available here .此处提供一个示例。

Whenever ember find an unknown helper name, it tries to resolve it with ComponentLookup .每当 ember 找到一个未知的助手名称时,它都会尝试使用ComponentLookup来解析它。 In your case, the component template is successfully resolved but it cannot find your component in the container and register/assign a default Ember.Component which does not implement your action handler.在您的情况下,组件模板已成功解析,但无法在容器中找到您的组件并注册/分配未实现您的操作处理程序的默认 Ember.Component。

try this:尝试这个:

create a view:创建视图:

MainAppView = Ember.View.extend({
   actions: {
       dummy_action: function() {
          console.log('dummy_action fired!');
      } 
    } 
});

then in your template:然后在您的模板中:

{{#view MainAppView}}
   <p>dummy component</p>
   <button {{action dummy_action target="view"}}>Dummy action</button>

{{/view}}

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

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