本文介绍了角度服务中的 EventEmitter,好还是坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Angular 服务中使用了 EventEmitter@Output,今天一位同事提到这不是一个好习惯.

I was using EventEmitter and @Output in Angular services, today one of the coleagues mentioned it's not a good practice.

我发现这篇帖子提到这是一种不好的做法,而且似乎主要是个人意见,而这个 提到可以使用它.我找不到任何关于它的官方文档,所以如果有人知道它的官方答案,请发布.

I found this post mentioning it's a bad practice and it seems mostly is personal opinion, and this answer is mentioning it's OK to use it.I couldn't find any official document about it, so if somebody knows an official answer for it please post.

关于EventEmittter的官方文档

推荐答案

注解 @Output() 在服务中没有作用.它用于告诉 Angular 模板编译器将 Observable 绑定到模板表达式.

The annotation @Output() has no effect in a service. It is used to tell the Angular template compiler to bind an Observable to a template expression.

如果我在服务中看到 @Output(),那么我会告诉开发人员将其删除.

If I saw @Output() in a service, then I'd tell the developer to remove it.

EventEmitter 是一个 Observable,在服务中使用它没有副作用,但也没有任何好处.

EventEmitter is an Observable and there are no side effects in using it in a service, but there are also no benefits.

您可以在组件或服务中使用任何 Observable 类型的发射器.我们有 EventEmitter 有两个原因.1) 它早于 Angular 团队决定使用 observable 的决定,他们认为他们可能需要自己的实现,2) 它可以在下一个 JavaScript 周期中发出值(可选设置).

You can use any Observable type of emitter in either a component or service. There are two reasons why we have EventEmitter. 1) it pre-dates the Angular teams decision to commit to using observables and they thought they might need their own implementation, 2) it can emit values in the next JavaScript cycle (optional setting).

存在一些边缘情况,即人们需要在下一个周期中发出更改以避免更改检测出现问题.

There were edge cases were people needed to emit changes in the next cycle to avoid problems with change detection.

 @Injectable()
 export class MyService {
       public events: Subject<any> = new Subject();
 }

上述服务的问题在于任何人都可以从公共事件发出值.您希望您的服务成为处理发出值的唯一代码.

The problem with the above service is that anyone can emit values from the public events. You want your service to be the only code that handles emitting values.

 @Injectable()
 export class MyService {
       private _events: Subject<any> = new Subject();
       public get events(): Observable<any> {
           return this._event.asObservable();
       }
 }

以上更好,因为对 Subject.next(..) 的访问是私有的.消费者只能订阅 observable.

The above is better because access to the Subject.next(..) is private. Consumers can only subscribe to the observable.

如果您遵循 components 方法.它迫使你暴露你的发射器,这不是一个好主意.

If you followed the components approach. It forces you to expose your emitter which isn't a good idea.

@Injectable()
export class MyService {
       @Output()   // <<< has no effect
       public events: EventEmitter<any> = new EventEmitter();
       // ^^ makes the emitter public
}

如果要在模板中使用,组件需要将它们的属性设为 public,但对于服务而言,情况并非如此.

Components need to have their properties as public if they are to be used in templates, but this isn't the case for services.

这篇关于角度服务中的 EventEmitter,好还是坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 07:39