Angular 6: Property 'catch' does not exist on type 'Observable'?

前端 未结 8 1618
情深已故
情深已故 2020-12-15 18:39

I am upgrading my app to Angular 6. I am upgrading from Angular 4, but the code below is causing errors in Angular 6, where it worked fine

相关标签:
8条回答
  • 2020-12-15 19:42

    This worked for me, I'am using Angular 6.1.0.

    import { Observable, Subject, of } from 'rxjs';
    import { switchMap, debounceTime, distinctUntilChanged, catchError } from 'rxjs/operators';
    
    this.ofertas = this.subjectPesquisa // Retorno Oferta[]
      .pipe(debounceTime(1000)) // Executa a ação do switchMap após 1 segundo
      .pipe(distinctUntilChanged()) // Apenas executa a ação switchMap se o termo enviado for outro
      .pipe(switchMap((termo: string) => {
    
        if (termo.trim() === '') {
          // Retornar um observable de array de ofertas vazio.
          return of<Oferta[]>([]);
        }
    
        console.log('Requisição HTTP para api: ', termo);
        return this.ofertasService.pesquisaOfertas(termo);
      }))
      .pipe(catchError((err: any) => {
        console.log('Erro: ', catchError);
        return of<Oferta[]>([]);
      }));
    
    0 讨论(0)
  • 2020-12-15 19:43
    import 'rxjs/add/operator/catch';
    

    Or import Observable this way:

    import {Observable} from 'rxjs';
    
    0 讨论(0)
提交回复
热议问题