Только тип для interface надо сделать, как у вас. Поля будут (id, date) вместо моих в интерфейсе:
service:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Todo } from './app.interface';
@Injectable()
export class AppService {
constructor(private httpClient: HttpClient) {}
fetchDataApi(url) {
return this.httpClient.get<Todo>(url)
}
}
component:
import { Component } from '@angular/core';
import { AppService } from './app.service';
import { Todo } from './app.interface';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
public users: Todo;
constructor(private httpService: AppService) {}
ngOnInit() {
this.loadData();
}
loadData() {
const API_URL = 'https://jsonplaceholder.typicode.com/todos/';
this.httpService.fetchDataApi(API_URL)
.subscribe(data => {
this.users = data
})
}
}
interface:
export interface Todo {
userId: number,
id: number,
title: string,
completed: boolean,
}