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

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

问题描述

我读过像<一个问题href=\"http://stackoverflow.com/questions/36059564/access-eventemitter-service-inside-of-customhttp\">Access CustomHttp 内EventEmitter服务
在用户使用EventEmitter在他的服务,但他在这个<建议href=\"http://stackoverflow.com/questions/36059564/access-eventemitter-service-inside-of-customhttp#comment59766410_36059564\">comment
不使用它,并直接使用,而不是观测量他的服务。

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.

我也阅读了本
<一href=\"http://stackoverflow.com/questions/36014395/angular-2-two-way-communication-between-components/36014451#36014451\">question
那里的解决办法建议以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?

推荐答案

EventEmitter 是一个angular2抽象和它的唯一目的是为发射在组件的事件。引用一个<一个href=\"http://stackoverflow.com/questions/33380696/unable-to-figure-out-correct-eventemitter-or-observable-syntax-in-angular2-servi/33382275#comment54684990_33382275\">comment来自Rob沃莫尔德

No, you should not subscribe manually to it.

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

[...] EventEmitter确实是一个抽象的角度,而应在组件发射的定制活动多只用pretty。否则,只是使用接收,就好像它是任何其它库。

[...] 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将继续成为一个可观察的。因此,这意味着我们的重构code。如果它的变化。我们必须访问的唯一API是其发出()方法。我们永远不应该手动订阅到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.

所有上述较为明确在这间病房贝尔的<一个href=\"http://www.bennadel.com/blog/3038-eventemitter-is-an-rxjs-observable-stream-in-angular-2-beta-6.htm#comments_47949\"相对=nofollow>注释(推荐阅读文章,而<一个href=\"http://www.bennadel.com/blog/3045-ward-bell-do-not-expect-eventemitter-to-be-observable-in-angular-2.htm\"相对=nofollow>回答到注释)。引用,以供参考。

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不要指望继续成为可观察到的!

Do NOT count on EventEmitter continuing to be an Observable!

不要指望这些观测运营​​商在未来在那里!

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

这将是德precated发布之前很快,可能删除。

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()

他的评论是在与Rob的评论很久以前行。

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>
    `,
    directives : [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.

TL; DR 答:

不,不要手动订阅它们,不要在服务中使用它们。如文档中仅显示发出的组件事件使用它们。不要损坏棱角分明的抽象。

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

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