请查看此成员旋转。
我想要的是为组件提供具有自定义操作的自定义按钮。组件本身不知道所涉及的操作,这些操作预计会冒泡到相应的控制器。
此代码在ember 2.8.0中有效,但在当前发布版本(2.8.2(中无效。请使用twidle在emberjs"2.8.0"one_answers"release"之间进行更改。这是emberjs错误吗?
是的,这只是ember 2.8.0版本中的Bug。这在下一个版本2.8.1中得到了修复。相关链接:https://github.com/emberjs/ember.js/pull/14291/files
对于您的用例,我创建了旋转
在应用程序控制器、中执行操作
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
actions:{
buttonMethod(){
console.log('buttonMethod');
}
}
});
创建我的按钮组件,以便它可以包含在任何组件中。这将调用从父组件传递的闭包操作。
在my-button.hbs
中
<button {{action buttonMethod }}> ButtonComponent </button>
{{yield}}
创建my-component
组件并向my-button
组件发送关闭操作按钮方法。在my-component.hbs
中
{{my-button buttonMethod=buttonMethod }}
{{yield}}
在application.hbs
中,这是我们在控制器中创建buttonMethod
动作的闭包动作的地方。所以我们需要将这个buttonMethod
属性一直传递到按钮组件。
{{my-component buttonMethod=(action 'buttonMethod') }}