在连接服务器期间发生错误:
src / app / components / add-news / add-news.component.ts(29,5)中的错误:错误TS2322:键入'{title:string; dateCreated:日期; longText:字符串; }”不能分配给“ IAddNews []”类型。
类型'{title:string;中缺少属性'includes' dateCreated:日期; longText:字符串; }'。
src / app / components / add-news / add-news.component.ts(54,7):错误TS2322:键入'{title:string; dateCreated:日期; longText:字符串; }”不能分配给“ IAddNews []”类型。
src / app / components / dashboard / dashboard / dashboard.component.ts(23,7):错误TS2322:类型“对象”不能分配给类型“ IAddNews []”。
“对象”类型可分配给很少的其他类型。您是要改用“ any”类型吗?
类型“对象”中缺少属性“包含”。
代码: add-news.component.ts:
ngOnInit() {
this.news = this.setEmptyNews();
}
setEmptyNews() {
return {
title: '',
dateCreated: new Date(),
longText: ''
};
}
addNews() {
this.news.push(this.setEmptyNews());
}
removeNews(index) {
if (this.news.length > 0) {
this.news.slice(index, 1);
}
}
onSubmit(addNewsForm) {
this.http.post('http://project.usagi.pl/news', this.news, this.httpOptions) .toPromise() .then((response: IAddNews) => {
console.log(response);
this.news = this.setEmptyNews();
});
}
}
dashboard.component.ts:
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.scss']
})
export class DashboardComponent implements OnInit {
httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': '70'
})
};
news: IAddNews[];
constructor(private http: HttpClient) {
this.http.get('http://project.usagi.pl/news', this.httpOptions).toPromise().then((response) => {
this.news = response;
console.log(response);
});
}
ngOnInit() {
}
removeNews(id){
this.news = this.news.filter(({newsId}) => newsId !== id);
this.deleteNewsServer(id);
console.log(id);
}
deleteNewsServer(deleteNewsId) {
this.http.post('http://project.usagi.pl/news/delete/' + deleteNewsId, this.news, this.httpOptions) .toPromise() .then((response: IAddNews) => {
console.log(response);
});
}
}
IAddNews:
export class IAddNews {
newsId?: string;
title: string;
longText: string;
shortText?: string;
dateCreated: any;