Error with operator 'map'

0

I'm starting now with Angular 6.

When I tried to create a service to consume data from an api, the following error appeared:

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Observable } from 'rxjs';

import { CategoriaModel } from './categoria/categoria.model';
import { PRODUTOS_API } from './../../app.api';

@Injectable()
export class CategoriasService{
constructor(private http: Http){}

categorias(): Observable<CategoriaModel[]>{
    return this.http.get('${PRODUTOS_API}/categorias').map(response => response.json())
}

}

Error: Property 'map' does not exist on type 'Observable'.

What am I doing wrong?

    
asked by anonymous 12.06.2018 / 15:15

2 answers

0

If you use the HttpClient library instead of http you do not need to map to json. Another thing, you can use the generics and pass your interface that angular automatically infers the return type of your Observable.

constructor(private httpClient: HttpClient){}

categorias(){
    return this.httpClient.get<CategoriaModel[]>('${PRODUTOS_API}/categorias')
   }
}
    
12.06.2018 / 15:29
0

I managed to solve Eduardo Varga. Thanks for the guidance.

The component that receives the injection should look like this:

ngOnInit() {
this.categoriasService.categorias().subscribe(categorias => this.categorias = categorias)

}

    
12.06.2018 / 16:13