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

问题描述

我读过像Access CustomHttp 内EventEmitter服务
在用户使用EventEmitter在他的服务,但他在这个comment
不使用它,并直接使用,而不是观测量他的服务。

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

我也阅读了本
question
那里的解决办法建议以EventEmitter传递给孩子,订阅它。

I also read thisquestionwhere 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?

推荐答案

是一个angular2抽象和它的唯一目的是为发射在组件的事件。引用一个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的文档中表示真正清楚。

This is stated really clear in EventEmitter's documentation.

按指令和组件发出的定制事件使用。

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.

所有上述较为明确在这间病房贝尔的注释(推荐阅读文章,而回答到注释)。引用,以供参考。

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 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的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-02 21:36