I am using eventemitters in class A and extending it to class B. But when I try to trigger the event from class B, it does not identify the event. Both class A and class B are injectables. I have to work around the situation by injecting the class A into class B.
Does Not Work
import { Injectable, EventEmitter } from '@angular/core';
@Injectable()
export class A {
_sm: EventEmitter<any>;
constructor() {
this._sm = new EventEmitter();
}
}
.
import { Injectable, EventEmitter } from '@angular/core';
import { A } from '../svc/class.a.service';
@Injectable()
export class B extends A {
constructor() {
super();
}
triggerEvent(){
this._sm.emit({});
}
}
===
Works
import { Injectable, EventEmitter } from '@angular/core';
@Injectable()
export class A {
_sm: EventEmitter<any>;
constructor() {
this._sm = new EventEmitter();
}
}
.
import { Injectable, EventEmitter } from '@angular/core';
import { A } from '../svc/class.a.service';
@Injectable()
export class B {
constructor(private _a: A) {
}
triggerEvent(){
this._a._sm.emit({});
}
}
.
Update of usage
import { Component, OnInit } from '@angular/core';
import { B } from '../svc/class.a.service';
@Component({
selector: 'app-menu',
templateUrl: './menu.component.html',
styleUrls: ['./menu.component.css']
})
export class MenuComponent implements OnInit {
constructor(private _es: B) {
this._es._sm.subscribe((data)=>{console.log('Testing Event')});
}
}
Is there an issue with extending an Injectable EventEmitter class? The reason why I am not keeping it a simple non-injectable class is because there are two such more classes M and C extending the class A. I want same instances of classes and events being captured by components using B, M, and C should be the same ones. They are inter-used and subscribed with multiple listeners.