I just started learning Angular. I found a guide in YouTube but while I was copying the code there was an error.
My task
- I need to get data from fakestoreapi.com/products that matches my interface IProduct
- Elements should be 5
- Should be a 2 second delay
My code: `
import { Injectable } from "@angular/core";
import {HttpClient, HttpErrorResponse, HttpParams} from "@angular/common/http";
import {catchError, delay, Observable} from "rxjs";
import {IProduct} from "../models/product";
@Injectable({
providedIn: 'root'
})
export class ProductsService {
constructor(private http: HttpClient) {
}
getAll(): Observable<IProduct[]> {
return this.http.get<IProduct[]>("https://fakestoreapi.com/products", {
params: new HttpParams({
fromObject: {limit: 5}
}).pipe( //error
delay(2000)
)
});
}
}
`
I didn't find this method in documentation, but it was in guide!
Screenshot of guide: enter image description here
CodePudding user response:
The pipe
part is positioned at the wrong place.
return this.http
.get<IProduct[]>("https://fakestoreapi.com/products", {
params: new HttpParams({
fromObject: {limit: 5}
})
})
.pipe( //error
delay(2000)
);
Check the official documentation of Angular Http Client