Angular HttpClient JSON-Service minimales Beispiel
English
Deutsch
Die Verwendung von Angulars HttpClient in einem Service zum Abrufen eines JSON ist ziemlich unkompliziert. In diesem Beispiel rufen wir ein JSON-Array ab.
Beachten Sie, dass es empfohlen wird, Ihr eigenes TypeScript-Interface für verbesserte statische Typisierung zu erstellen, aber dennoch ist der Start mit any[] oft eine gute Idee, da er Ihnen einen schnellen Einstieg ermöglicht.
myservice_load_json.ts
public loadMyJSONArray(): Observable<any[]> {
return this.http.get<any[]>(`/api/my-api`);
}Vollständiges Service-Beispiel
myservice.service.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class MyService {
constructor(private http: HttpClient) {
}
public loadMyJSONArray(): Observable<any[]> {
return this.http.get<any[]>(`/api/my-api`);
}
}Check out similar posts by category:
Angular, Typescript
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow