我正在尝试为我的 Ember 组件定义自定义操作并希望在父/子之间进行通信。
在 my-child.js 中,我有
actions: {
someCustomAction: function() {
let self = this;
self.sendAction('someCustomAction');
},
}
我在 my-parent.js 中发现了同样的情况,如下所示;
actions: {
someCustomAction: function (){
console.log("Inside someCustomAction....");
}
}
现在,使用上面的代码,控制/操作不会到达 my-parent.js
我必须将“someCustomAction”添加到模板中,如下所示
在 my-parent.hbs 中
{{my-child someCustomAction="someCustomAction"}}
我想知道确切的原因。为什么只执行 sendAction 不能自动工作?
最佳答案
以下是我在遵循数据向下、操作向上 (DDAU) 模式时的做法。
首先,您使用 action helper 传递您想要在 child 中触发的操作。
{{my-child someActionName=(action 'parentAction')}}
然后,您检索该操作并从子组件中触发它。
this.get('someActionName')(params);
关于javascript - Ember 将操作添加到模板以进行父/子通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46115725/