I have this part in a function :
this.myservice.getComments()
.subscribe(data => {
this.post.comments = data.body;
this.post.comments_count = Number(data.headers.get('x-wp-total'));
this.post.comments_pages = Number(data.headers.get('x-wp-totalpages'));
this.content_ready = true;
});
The getComments
function :
export class myservice extends DataService {
protected url = 'my_url';
constructor( protected http: HttpClient ) {
super(http);
}
getComments(){
return this.get(
this.subUrl, true );
}
}
Relevant part of dataService :
export class DataService {
protected url: string;
constructor(protected http: HttpClient) {}
get(subUrl:string = '', needObservable = false) {
let options = {};
if(needObservable) {
options = {observe: 'response'};
}
return this.http.get(this.url + subUrl, options);
}
}
All of this works well. The thing is, my IDE (phpstorm) is complaining about the data.headers
and data.body
, arguing that those properties does not exists on type 'object'.
How to let it know that everything is fine ? I thought about typing the return but not succeeding in doing so.
In Angular, Observable instances are used to specify subscriber functions. Any time a consumer wants to execute the function, the subscribe () method is called. The subscribe () method provides a way to retrieve messages and values to be published. Let’s create new angular project to play around with using Observables in Angular.
An observable is a technique to handle sharing data. It’s considered the better version of a promise and is used extensively throughout Angular. Where a promise can only return a single value, an observable can return a stream of values. A promise cannot be cancelled, but an observable can be.
The return type is Observable<SearchItem[]>, it’s going to return an observable where each item in the observable is SearchItem[], each item in the observable is going to be an array of SearchItems.
The return type is Observable<SearchItem []>, it’s going to return an observable where each item in the observable is SearchItem [], each item in the observable is going to be an array of SearchItems.
This is not related to the IDE. Just Typescript's compiler is arguing that type object
does not has those properties and it is right.
Just set the type of data
to any
- data: any
.
.subscribe((data: any) => {
this.post.comments = data.body;
this.post.comments_count = Number(data.headers.get('x-wp-total'));
this.post.comments_pages = Number(data.headers.get('x-wp-totalpages'));
this.content_ready = true;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With