【发布时间】:2021-08-18 17:49:07
【问题描述】:
在我的 Angular 客户端中,我有一个模块调用库。它执行与书籍相关的 CRUD 交易。我在应用程序根目录中有一个警报组件和服务。该警报组件作为视图子嵌套在 app-component 中。因此,一旦我执行了 CRUD 操作,我想从我的库模块通知 root 中的服务。
建立这种沟通的最简单方法是什么?
警报服务
import { Injectable } from '@angular/core';
//Alert service
@Injectable({
providedIn: 'root'
})
export class AlertService {
//Attributes
show: boolean = false;
type: string = 'info';
message: string;
timeoutTimer: any;
//Constructor of the AlertService.
constructor() { }
//Responsible for displaying the alert.
displayAlert(message: string, type: string = 'info', autohide: number = 5000) {
this.show = true;
this.message = message;
this.type = type;
//If timer is already available
if (this.timeoutTimer) {
clearTimeout(this.timeoutTimer);
}
//If autohide is there then start the timeout
if (autohide) {
setTimeout(() => {this.close()}, autohide);
}
}
//Responsible for setting the show attribute false.
close() {
this.show = false;
}
}
【问题讨论】:
标签: angular typescript angular-services angular-components angular-event-emitter