Hice el recorrido de los héroes y quiero hacer algo similar, pero tengo un problema para entender angular-in-memory-web-api. Mira mi código: clients-data.service.ts
import { Injectable } from '@angular/core'; import { InMemoryDbService } from 'angular-in-memory-web-api'; @Injectable({ providedIn: 'root' }) export class ClientsDataService implements InMemoryDbService{ createDb(){ const clients = [ {id: 1, name: 'Jan Kowalski'}, {id: 2, name: 'Grzegorz Brzęczyszczykiewicz'}, {id: 3, name: 'Paweł Nowak'}, ]; return clients; } constructor() { } }clientes.servicio.ts
import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Observable, of } from 'rxjs'; import { Client } from './client'; @Injectable({ providedIn: 'root' }) export class ClientService { private clientsURL = 'api/clients'; constructor( private http: HttpClient, ) { } getClients(): Observable<Client[]> { return this.http.get<Client[]>(this.clientsURL) } }aplicación.módulo.ts
//... HttpClientInMemoryWebApiModule.forRoot( ClientsDataService, { dataEncapsulation: false } ) //...Pero en la consola muestra un error 404 para api/client. ¿Dónde me equivoco?
su createDb() necesita devolver un objeto de pares clave/valor, no una matriz.
createDb() { const clients = [ {id: 1, name: 'Jan Kowalski'}, {id: 2, name: 'Grzegorz Brzęczyszczykiewicz'}, {id: 3, name: 'Paweł Nowak'}, ]; return { clients: clients } } O simplemente cambie su línea de devolución para return {clients};
Creo que el error está en tu camino.
private clientsURL = 'http://entire-url/api/clients';Revisa toda tu ruta, quizás el error esté ahí.