本文介绍了如何退订Angular 2中的EventEmitter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

export declare class EventEmitter<T> extends Subject<T> {
    /**
     * Creates an instance of [EventEmitter], which depending on [isAsync],
     * delivers events synchronously or asynchronously.
     */
    constructor(isAsync?: boolean);
    emit(value: T): void;
    /**
     * @deprecated - use .emit(value) instead
     */
    next(value: any): void;
    subscribe(generatorOrNext?: any, error?: any, complete?: any): any;
}

在Angular 2 Typescript官方定义中,似乎无法静音或取消订阅EventEmitter.

In Official Angular 2 Typescript definition, seems it has no way to mute or unsubscribe from EventEmitter.

随着页面使用相同的EventEmitter,我得到了一段时间的回调

I got callback over time as pages use the same EventEmitter

推荐答案

EventEmitter扩展了Subject.订阅主题时,您会得到一个Subscription,以后可以用来取消订阅.

EventEmitter extends Subject. When you subscribe to a subject you get a Subscription which you can later use to unsubscribe.

someOutput:EventEmitter = new EventEmitter();
...
this.subscription = someOutput.subscribe(...);
...
this.subscription.unsubscribe();

提示
@Output()之外,请勿将EventEmitter用作其他任何内容. Angular不保证EventEmitter将来会继续扩展Subject甚至与Subject相似.

Hint
Don't use EventEmitter for anything else but @Output()s. Angular doesn't guarantee that EventEmitter will keep extending Subject or even work similar to a Subject in the future.

这篇关于如何退订Angular 2中的EventEmitter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 07:32