0
的组件显示要了解有关使用服务和组件的更多信息,我试图设置一个简单的Flash消息样式服务。在我的路线中,我保存了一条记录并从服务器接收到json通知或json错误。由于这是一个行为,我最终想使用应用程序范围,我使用一个简单的服务(注入我的路线)来处理显示消息。在路线内,我可以拨打this.get('notification').displayMessage(msg)
。更新来自路由
目前服务中的displayMessage
函数只是提醒消息,因为我被困在如何创建服务可以“更新”的组件。服务如何与组件通信以便我可以发送消息并从组件模板中显示消息?
简档/索引路线
user.save().then((response) => {
//display response.notice in the app-notification component template
}, (response) => {
let errors = JSON.parse(response.errors);
//display response.error in the app-notification component template
this.get('notification').displayMessage(errors[0]);
}
服务
import Ember from 'ember';
const { Service } = Ember;
export default Service.extend({
displayMessage(msg) {
alert("message ---> " + msg);
}
});
部件
???
分量模板
<h2 class="alert">{{message}}</h2>
应用模板
{{app-notification message=message}}