简而言之,该想法是将错误与UI逻辑完全脱钩:
- 创建一个通知(烤面包机)组件,该组件将订阅错误,警告,信息性,成功消息
- 创建一个能够发送消息和通知组件以使用它们的服务。
示例notification.service.ts
:
import { Injectable } from '@angular/core'
import { BehaviorSubject,Subject } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class NotificationService {
private readonly errorsSubject$ = new Subject<string>();
public errors$() {
return this.errorsSubject$.asObservable();
}
public showError(message: string) : void {
this.errorsSubject$.next(message);
}
}
示例notification.component.ts
,在您的应用程序中应该只有一个实例。
import { Component,Input } from "@angular/core";
import { NotificationService } from "./notification.service";
@Component({
selector: "notification",template: `
<h2 *ngIf="(error$ | async) as error">Hello {{ error }}!</h2>
`,styles: [
`
h1 {
font-family: Lato;
}
`
]
})
export class NotificationComponent {
error$ = this.notificationService.errors$();
constructor(private readonly notificationService: NotificationService) {}
}
也是可以发送消息的示例组件,可以是执行此操作的任何其他组件。
import { Component,Input } from "@angular/core";
import { NotificationService } from "./notification.service";
@Component({
selector: "hello",template: `
<button (click)="onClick()">Show error</button>
`,styles: [
`
button {
font-family: Lato;
}
`
]
})
export class HelloComponent {
@Input() name: string;
constructor(private readonly notificationService: NotificationService) {}
onClick(): void {
this.notificationService.showError(
`This error has been posted on ${Date.now()}`
);
}
}
因此,现在只要您在任何组件中注入通知服务并通过该服务发送消息,通知组件就可以订阅它并在全局显示它们。这是Stackblitz,表明它可以正常工作。
显然,这是非常简化的,您需要执行更多的工作,但这应该使您走上正轨。
一个改进是从通知服务中删除error$
可见的内容,仅允许Notification组件访问它。您可以通过实现内部通知服务来实现这一点,该服务将充当通知服务和通知组件之间的桥梁。你赢了什么?通知服务不再公开error$
可见的信息,而只公开发送消息的方法。
notification.service.ts
import { Injectable } from "@angular/core";
import { Subject } from "rxjs";
import { NotificationInternalService } from "./notification-internal.service";
@Injectable({
providedIn: "root"
})
export class NotificationService {
constructor(private readonly internalService: NotificationInternalService){}
public showError(message: string): void {
this.internalService.errorSubject$.next(message);
}
}
notification-internal.service.ts
import { Injectable } from "@angular/core";
import { Subject } from "rxjs";
@Injectable({
providedIn: "root"
})
export class NotificationInternalService {
public errorSubject$ = new Subject<string>();
public get error$() {
return this.errorSubject$.asObservable();
}
}
并且notification.component.ts
现在引用了notification-internal.service.ts
import { Component,Input } from "@angular/core";
import { NotificationInternalService } from "./notification-internal.service";
@Component({
selector: "notification",styles: [
`
h1 {
font-family: Lato;
}
`
]
})
export class NotificationComponent {
error$ = this.service.error$;
constructor(private readonly service: NotificationInternalService) {}
}
本文链接:https://www.f2er.com/3164263.html