#Pitfalls of using observables and other subjects as @Output

3 messages · Page 1 of 1 (latest)

graceful sail
#

Recently I've found out that you can use any observable on @Output-decorated property and it will work. I wonder if there are any potential pitfalls?

// Both components and directives
@Component({...})
export class MyComponent {

  // Any subject/observable as output
  @Output()
  public myEvent = fromEvent(this.elRef.nativeElement, 'click').pipe(
    map(() => 'Clicked!'),
    finalize(() => console.log('unsubscribed!'))
  );

  constructor(private elRef: ElementRef<HTMLElement>) {}
}
frigid creek
#

yes you can. eventemitter extends from subject. So that's exactly the same as doing

@output xxx

fromEvent().subcribe((e)=> this.xxx.next(e))