EmberJS:没有处理动作

EmberJS: Nothing handled the action

Ember新手。使用 2.3.0。对于现有的应用程序,我需要添加一个按钮来切换屏幕上和屏幕外的内容。按照现有代码模式,我添加了以下两个文件。

app/components/help.js

import Ember from 'ember';

export default Ember.Component.extend({
    actions: {
      toggleHelp() {
          this.toggleProperty('isShowingHelp');
      }
    }
});

templates/components/help.hbs

<div class="help">
    <a href="#" {{action "toggleHelp"}}>help</a>
</div>
{{#if this.isShowingHelp}}
  <p>HELPHELPHELP</p>
{{/if}}

然后在各种 *.hbs 文件中添加 {{partial "components/help"}} 以使按钮显示在需要的位置。

按钮按预期呈现

<div class="help">
    <a href="#" data-ember-action="493">help</a>
</div>

但是当我点击的时候,我得到了错误,"Nothing handled the action 'toggleHelp'. If you did handle the action, this error can be caused by returning true from an action handler in a controller, causing the action to bubble."

我检查了 Ember documentation,发现这与说明相符。

我认为问题可能是因为我将其称为 partial,但 none 其他选项(viewrenderoutlet) 似乎适用于我的有限情况。

根据this answer,我尝试了{{action "toggleHelp" target='component'}},但是当我点击时的错误变成了,"ember.debug.js:29112 Uncaught TypeError: Cannot read property 'send' of undefined."

我完全不知所措...任何人都可以指出我在这里缺少什么吗?谢谢!

更新:

如果我将 toggleHelp 与其中一个 .hbs 文件一起复制到控制器中,则 link 可以正常工作。但正确的方法肯定不是把它放在多个控制器文件中吗? (当我尝试将 toggleHelp 放入 controllers/application.js 中,希望能覆盖所有地方时(?!),我又回到了最初的错误。)

你的问题确实是{{partial "components/help"}}!

一般来说你不应该使用偏音。要调用组件,您可以使用 {{component-name}}.

但是 Ember 2.3 中的组件名称中需要包含 -。所以 help 有效!

将您的组件重命名为类似 my-help 的名称。所以 app/components/my-help.js 用于你的 js 和 templates/components/my-help.hbs。然后你可以做 {{my-help}} 来使用这个组件。