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