I am getting a 404 not found error on this line of code "return this.http.get('../data/products.json');"
where the path is "src\app\shared\services\product.service.ts" for the service.ts file. The path of the json file is "src\data\products.json"
This is the service.ts code:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
export interface Product {
id: number;
title: string;
price: number;
imageUrl: string;
description: string;
}
@Injectable()
export class ProductService {
constructor(private http: HttpClient) {}
getAll(): Observable<Product[]> {
return this.http.get<Product[]>('../data/products.json');
}
getById(productId: number): Observable<Product> {
return this.http.get<Product[]>('../data/products.json')
.pipe(
map(products => <Product>products.find(p => p.id === productId))
);
}
}
Try to put your json folder under an assets folder like so :
src\assets\mock\products.json
and then you modify the path in your method :
getAll(): Observable<Product[]> {
return this.http.get<Product[]>('assets/mock/products.json')
}