i have a API which return JSON data of posts. I am trying to fetch these data from Angular 5 with this approach.
I declare a Post interface
export interface Post {
userId:number;
id:number;
title:string;
body:string
}
and my post.service.ts file
import { Injectable } from '@angular/core';
import {Post} from './post';
import { HttpClient, HttpHeaders, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { catchError, map, tap } from 'rxjs/operators';
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
@Injectable()
export class PostService {
private posturl = 'http://jsonplaceholder.typicode.com/posts/';
constructor(private http:HttpClient) {
}
getData():Observable<Post[]> {
return this.http.get<Post[]>(this.posturl)
.pipe(
catchError(this.handleError())
);
}
}
but in service file i am getting following error. like 'Observable<{}>' is not assignable to type.....
- how to get json data through httpclient.get.
- How can i access data data in my post module ts file from observable object which is return by getData function of post.service.ts