The 'this' context of the EventEmitterstring is not attributable to the 'this' method of type Observablestring

0

I looked at the example in a GitHub repository. The application runs successfully and the written code works fine, but I'm having the following error in my Visual Studio Code:

  

The 'this' context of type' EventEmitter 'is not assignable to method's' this' of type' Observable '. \ n Types of property' lift 'are incompatible. \ n Type' (operator: Operator) = > Observable 'is not assignable to type' (operator: Operator) = > \ N Type 'string' is not assignable to type 'R'. \ N Type 'Observable' is not assignable to type 'Observable'.

The code that is generating the error is as follows:

this.notificationService.notifier
    .do(message => {
      this.message = message;
      this.snackVisibility = 'visible'; 
    })
    .switchMap(message => Observable.timer(2000))
    .subscribe(timer => this.snackVisibility = 'hidden');

Although the application works perfectly VS Code gives me the error as shown in the image:

What would be the best solution to this error?

    
asked by anonymous 04.11.2018 / 02:25

1 answer

0

After doing some research, I understood that EventEmitter is used by a Child par aum Parent. To make service communications for the component it is necessary to use the Subject.

Then the component code looks like this:

this.notificationService.notifier$
                        .subscribe(message => {
                          this.message = message;
                          this.snackVisibility ='visible';
                          setTimeout(() => {
                            this.snackVisibility = 'hidden';
                          }, 2000);
                        });

And the service code looks like this:

import { Subject } from "rxjs/Subject";

export class NotificationService {
    private notifier = new Subject<string>();
    public notifier$ = this.notifier.asObservable();

    notify(message: string) {
        this.notifier.next(message);
    }
}
    
05.11.2018 / 18:53