API Data -> Adapter -> Model > App
[
{
"userId": 1,
"id": 1,
"title": "quidem molestiae enim"
},
{
"userId": 1,
"id": 2,
"title": "sunt qui excepturi placeat culpa"
}
]
export class AlbumService {
private baseUrl = "https://jsonplaceholder.typicode.com/albums";
constructor(private http: HttpClient) {}
list(): Observable<Album[]> {
const url = this.baseUrl;
return this.http.get<any[]>(url).pipe(
map(data => data.map(Album.adapt))
);
}
}
export class Album {
constructor(
public userId: number,
public id: number,
public title: string,
) {}
static adapt(item: any): Album {
return new Album(
item.id,
item.userId,
item.title,
);
}
}
npm install @angular/cli
ng server
- Access localhost:4200