我在 ember 中有一个组件,它需要向应用程序 Controller 发送一个 Action (带有一个参数)。无论该组件在哪里呈现,它都需要在应用程序 Controller 上调用完全相同的操作。
应用程序 Controller
export default Ember.Controller.extend({
actions: {
addAlert: function(message) {
this.set('message', message);
},
removeAlert: function(message) {
this.set('message', message);
}
}
});
我该如何处理?从头到尾。
最佳答案
Action 不会通过 Controller 向上冒泡,当一个 Action 被触发时,它将通过当前路由的 Controller ,如果那里没有任何东西处理它,它会向上冒泡到当前路由,一直到顶层路由(应用程序) .
如果该操作必须在 Controller 上设置一个属性,您可以直接从应用程序路由中设置它(尽管不推荐这样做)。
// routes/application.js
actions {
addAlert(message) {
this.controller.set('message', message);
},
removeAlert(message) {
this.controller.set('message', message);
}
}
有关更多信息,请阅读 action bubbling .
关于javascript - Ember : Bubble action from component to application controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31853437/