Reputation: 2069
I'm created my own Observable service
import { Injectable, EventEmitter, Output} from '@angular/core';
@Injectable()
export class ObservableService {
data = [];
@Output eventEmitter:EventEmitter = new EventEmitter();
setSharedData(key, value) {
this.data[key] = value;
this.eventEmitter.emit(this.data);
}
getSharedData() {
return this.data;
}
}
And here is an usage example
ngOnInit() {
this._observable.eventEmitter.subscribe((data) => {
console.log(data);
})
}
So currently while compiling it saying
app/services/data-observable.service.ts(6,5): error TS1240: Unable to resolve signature of property decorator when called as an expression.
Supplied parameters do not match any signature of call target.
Which is refers to this particular string,
@Output eventEmitter:EventEmitter = new EventEmitter();
But the service works perfect, any suggestions what is wrong?
Upvotes: 2
Views: 3233
Reputation: 71961
You are missing the parentheses after Output()
. Although Output()
is not necessary within a service. This is only necessary in a component or directive, because then you can use the (eventEmitter)="onEventEmit($event)"
notation within a template.
On the other hand you are missing a type annotation for the generic EventEmitter<T>
:
eventEmitter: EventEmitter<any> = new EventEmitter();
or
eventEmitter: EventEmitter<boolean> = new EventEmitter<boolean>();
Above that, EventEmitters
should only be used inside @Component
. If you want similar functionality, use Subject
from rxjs
.
subject: Subject<boolean> = new Subject<boolean>();
Upvotes: 5