I am getting this error with my component not sure what to do?
here is the component:
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-emails',
templateUrl: './emails.component.html',
styleUrls: ['./emails.component.scss']
})
export class EmailsComponent implements OnInit {
test = 'test';
results: string[];
constructor(private http: HttpClient) { }
ngOnInit() {
interface ItemsResponse {
results: string[];
}
this.http.get<ItemsResponse>('assets/api/email_list.json').subscribe(data => {
this.results = data;
console.log(this.results);
});
}
}
and then I am getting this error, oddly enough, is working in the browser even with the error?
ERROR in src/app/components/emails/emails.component.ts(24,7): error TS2322: Type 'ItemsResponse' is not assignable to type 'string[]'.
src/app/components/emails/emails.component.ts(24,7): error TS2322: Type 'ItemsResponse' is not assignable to type 'string[]'.
Property 'includes' is missing in type 'ItemsResponse'.
edit: added json snippet:
[
{
"pk": "wGR",
"created": "2017-10-07T01:42:25.110747Z",
"email_domain": "domain.com",
"sender_name": null,
"sender_email": "[email protected]",
"has_user_viewed": false,
"is_shielded": false
}
]
adding this so we know where we are at with this and for historical reasons so I learn something new here! current revision:
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-emails',
templateUrl: './emails.component.html',
styleUrls: ['./emails.component.scss']
})
export class EmailsComponent implements OnInit {
results: ItemsResponse[]
constructor(private http: HttpClient) { }
ngOnInit() {
interface ItemsResponse {
pk: string;
created: string;
email_domain: string;
sender_name: string;
sender_email: string;
has_user_viewed: boolean;
is_shielded: boolean;
}
this.http.get<ItemsResponse>('assets/api/email_list.json').subscribe(data => {
this.results = data.results;
console.log(this.results);
});
}
}
See Question&Answers more detail:os