0

Olhei o exemplo em um repositório do GitHub. A aplicação executa com sucesso e o código escrito funciona normalmente, mas estou com o seguinte erro no meu 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) => Observable'.\n Type 'Observable' is not assignable to type 'Observable'.\n Type 'string' is not assignable to type 'R'.

O código que está gerando o erro é o seguinte:

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

Apesar de a aplicação funcionar perfeitamente o VS Code me apresenta o erro como apresentado na imagem:

inserir a descrição da imagem aqui

Qual seria a melhor solução para este erro?

1
  • 1
    Se puder colocar o código do seu this.notificationService.notifier, por gentileza 5/11/2018 às 11:49

2 Respostas 2

0

Depois de fazer algumas pesquisas, entendi que EventEmitter é utilizado de um Child par aum Parent. Para fazer comunicações de serviços para o componente é necessário utilizar o Subject.

Então o código do componente ficou assim:

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

E o código do serviço ficou assim:

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);
    }
}
0

No seu service deve ter algo desse tipo:

notifier = new EventEmitter<string>();

Uma outra solução pode ser você trocar de string para any, pois "nesse cenário", faz sentido.

Ficaria dessa forma:

notifier = new EventEmitter<any>();

Um abraço!

Você deve fazer log-in para responder a esta pergunta.