fixed in-memory-data

This commit is contained in:
DESKTOP-8HAFCLV\Yugr 2025-06-24 10:11:52 +04:00
parent 00c700a0fa
commit 1e585195e9
5 changed files with 75 additions and 8 deletions

15
package-lock.json generated
View File

@ -14,6 +14,7 @@
"@angular/forms": "^20.0.0", "@angular/forms": "^20.0.0",
"@angular/platform-browser": "^20.0.0", "@angular/platform-browser": "^20.0.0",
"@angular/router": "^20.0.0", "@angular/router": "^20.0.0",
"angular-in-memory-web-api": "^0.20.0",
"rxjs": "~7.8.0", "rxjs": "~7.8.0",
"tslib": "^2.3.0" "tslib": "^2.3.0"
}, },
@ -3378,6 +3379,20 @@
} }
} }
}, },
"node_modules/angular-in-memory-web-api": {
"version": "0.20.0",
"resolved": "https://registry.npmjs.org/angular-in-memory-web-api/-/angular-in-memory-web-api-0.20.0.tgz",
"integrity": "sha512-piEcu1MvHOnPnESSFgzHEz9YUDxqQ1WbH8yp9f0sXr+//dTEEj/pY6YY7OthBSUP/DLTAEE4itE1SOYs0dOKMA==",
"license": "MIT",
"dependencies": {
"tslib": "^2.3.0"
},
"peerDependencies": {
"@angular/common": "^20.0.0",
"@angular/core": "^20.0.0",
"rxjs": "^6.5.3 || ^7.4.0"
}
},
"node_modules/ansi-escapes": { "node_modules/ansi-escapes": {
"version": "4.3.2", "version": "4.3.2",
"resolved": "https://registry.npmjs.org/ansi-escapes/-/ansi-escapes-4.3.2.tgz", "resolved": "https://registry.npmjs.org/ansi-escapes/-/ansi-escapes-4.3.2.tgz",

View File

@ -16,6 +16,7 @@
"@angular/forms": "^20.0.0", "@angular/forms": "^20.0.0",
"@angular/platform-browser": "^20.0.0", "@angular/platform-browser": "^20.0.0",
"@angular/router": "^20.0.0", "@angular/router": "^20.0.0",
"angular-in-memory-web-api": "^0.20.0",
"rxjs": "~7.8.0", "rxjs": "~7.8.0",
"tslib": "^2.3.0" "tslib": "^2.3.0"
}, },

View File

@ -1,10 +1,14 @@
import { ApplicationConfig, provideBrowserGlobalErrorListeners, provideZonelessChangeDetection } from '@angular/core'; import { ApplicationConfig, importProvidersFrom, provideBrowserGlobalErrorListeners, provideZonelessChangeDetection } from '@angular/core';
import { provideRouter } from '@angular/router'; import { provideRouter } from '@angular/router';
import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
import { routes } from './app.routes'; import { routes } from './app.routes';
import { provideHttpClient } from '@angular/common/http';
import { InMemoryDataService } from './in-memory-data.service';
export const appConfig: ApplicationConfig = { export const appConfig: ApplicationConfig = {
providers: [ providers: [
provideHttpClient(),
importProvidersFrom(InMemoryWebApiModule.forRoot(InMemoryDataService, { delay: 150 })),
provideBrowserGlobalErrorListeners(), provideBrowserGlobalErrorListeners(),
provideZonelessChangeDetection(), provideZonelessChangeDetection(),
provideRouter(routes) provideRouter(routes)

View File

@ -3,6 +3,7 @@ import { Hero } from './hero';
import { HEROES } from './mock-heroes'; import { HEROES } from './mock-heroes';
import { Observable, of } from 'rxjs'; import { Observable, of } from 'rxjs';
import { MessageService } from './message.service'; import { MessageService } from './message.service';
import { HttpClient } from '@angular/common/http';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@ -10,13 +11,23 @@ import { MessageService } from './message.service';
export class HeroService { export class HeroService {
constructor(private messageService: MessageService) { } private heroesUrl = 'api/heroes'; // URL to web api
getHeroes(): Observable<Hero[]> { constructor(
const heroes = of(HEROES); private http: HttpClient,
this.messageService.add('HeroService: fetched heroes'); private messageService: MessageService)
return heroes; { }
}
// getHeroes(): Observable<Hero[]> {
// const heroes = of(HEROES);
// this.messageService.add('HeroService: fetched heroes');
// return heroes;
// }
/** GET heroes from the server */
getHeroes(): Observable<Hero[]> {
return this.http.get<Hero[]>(this.heroesUrl)
}
getHero(id: number): Observable<Hero> { getHero(id: number): Observable<Hero> {
// For now, assume that a hero with the specified `id` always exists. // For now, assume that a hero with the specified `id` always exists.
@ -26,4 +37,8 @@ getHeroes(): Observable<Hero[]> {
return of(hero); return of(hero);
} }
/** Log a HeroService message with the MessageService */
private log(message: string) {
this.messageService.add(`HeroService: ${message}`);
}
} }

View File

@ -0,0 +1,32 @@
import { Injectable } from '@angular/core';
import { InMemoryDbService } from 'angular-in-memory-web-api';
import { Hero } from './hero';
@Injectable({
providedIn: 'root',
})
export class InMemoryDataService implements InMemoryDbService {
createDb() {
const heroes = [
{ id: 12, name: 'Dr. Nice' },
{ id: 13, name: 'Bombasto' },
{ id: 14, name: 'Celeritas' },
{ id: 15, name: 'Magneta' },
{ id: 16, name: 'RubberMan' },
{ id: 17, name: 'Dynama' },
{ id: 18, name: 'Dr. IQ' },
{ id: 19, name: 'Magma' },
{ id: 20, name: 'Tornado' }
];
return {heroes};
}
// Overrides the genId method to ensure that a hero always has an id.
// If the heroes array is empty,
// the method below returns the initial number (11).
// if the heroes array is not empty, the method below returns the highest
// hero id + 1.
genId(heroes: Hero[]): number {
return heroes.length > 0 ? Math.max(...heroes.map(hero => hero.id)) + 1 : 11;
}
}