EventEmitter 的正确用法是什么? [英] What is the proper use of an EventEmitter?

查看:30
本文介绍了EventEmitter 的正确用法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过诸如在 CustomHttp 中访问 EventEmitter 服务之类的问题用户在他的服务中使用 EventEmitter 的地方,但他在这个评论不要使用它,而是直接在他的服务中使用 Observables.

I've read questions like Access EventEmitter Service inside of CustomHttp where the user uses EventEmitter in his service, but he was suggested in this comment not to use it and to use instead Observables directly in his services.

我也读过这个问题解决方案建议将 EventEmitter 传递给孩子并订阅它.

I also read this question where the solution suggests to pass the EventEmitter to the child and subscribe to it.

我的问题是:我应该还是不应该手动订阅 EventEmitter?我应该如何使用它?

My question then is: Should I, or should I not subscribe manually to an EventEmitter? How should I use it?

推荐答案

TL;DR:

不,不要手动订阅它们,不要在服务中使用它们.按照文档中的说明使用它们仅用于在组件中发出事件.不要打败 angular 的抽象.

答案:

EventEmitter 是一个 angular2 抽象和它的唯一目的是在组件中发出事件.引用 评论 来自 Rob Wormald

EventEmitter is an angular2 abstraction and its only purpose is to emit events in components. Quoting a comment from Rob Wormald

[...] EventEmitter 实际上是一个 Angular 抽象,应该只用于在组件中发出自定义事件.否则,就像使用任何其他库一样使用 Rx.

[...] EventEmitter is really an Angular abstraction, and should be used pretty much only for emitting custom Events in components. Otherwise, just use Rx as if it was any other library.

这在 EventEmitter 的文档中说得很清楚.

This is stated really clear in EventEmitter's documentation.

由指令和组件使用以发出自定义事件.

Use by directives and components to emit custom Events.

使用它有什么问题?

Angular2 永远不会向我们保证 EventEmitter 将继续作为 Observable.所以这意味着如果我们的代码发生变化,就重构它.我们必须访问的唯一 API 是它的 emit() 方法.我们永远不应该手动订阅 EventEmitter.

What's wrong about using it?

Angular2 will never guarantee us that EventEmitter will continue being an Observable. So that means refactoring our code if it changes. The only API we must access is its emit() method. We should never subscribe manually to an EventEmitter.

上述所有内容在 Ward Bell 的 评论(推荐阅读文章,回答 该评论).引用供参考

All the stated above is more clear in this Ward Bell's comment (recommended to read the article, and the answer to that comment). Quoting for reference

不要指望 EventEmitter 继续成为 Observable!

Do NOT count on EventEmitter continuing to be an Observable!

不要指望将来会出现那些 Observable 操作符!

Do NOT count on those Observable operators being there in the future!

这些将很快被弃用,并可能在发布前被删除.

These will be deprecated soon and probably removed before release.

EventEmitter 仅用于子组件和父组件之间的事件绑定.不要订阅它.不要调用任何这些方法.只调用 eve.emit()

Use EventEmitter only for event binding between a child and parent component. Do not subscribe to it. Do not call any of those methods. Only call eve.emit()

他的评论与罗布很久以前的评论一致.

His comment is in line with Rob's comment long time ago.

只需使用它从您的组件发出事件即可.看看下面的例子.

Simply use it to emit events from your component. Take a look a the following example.

@Component({
    selector : 'child',
    template : `
        <button (click)="sendNotification()">Notify my parent!</button>
    `
})
class Child {
    @Output() notifyParent: EventEmitter<any> = new EventEmitter();
    sendNotification() {
        this.notifyParent.emit('Some value to send to the parent');
    }
}

@Component({
    selector : 'parent',
    template : `
        <child (notifyParent)="getNotification($event)"></child>
    `
})
class Parent {
    getNotification(evt) {
        // Do something with the notification (evt) sent by the child!
    }
}

如何不使用它?

class MyService {
    @Output() myServiceEvent : EventEmitter<any> = new EventEmitter();
}

停在那里......你已经错了......

Stop right there... you're already wrong...

希望这两个简单的例子能够阐明 EventEmitter 的正确用法.

Hopefully these two simple examples will clarify EventEmitter's proper usage.

这篇关于EventEmitter 的正确用法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆