0
我有以下類:Angular2地圖JSON類與注射器
import { HttpClient } from '../services/http.service'
export class Project {
constructor(
private http: HttpClient
){
}
@JsonProperty('name')
title: string;
id:string;
public getTitle(): string {
return this.title;
}
public getId(): string {
return this.id;
}
public removeProject() {
return this.http.delete("deleteurl"+this.id).map(() => {
return true;
})
}
}
我想反序列化JSON對象是這樣的:
import { JsonConvert } from "json2typescript";
getProjects():Observable<Project[]> {
return this.http.get("getprojects").map((res:Response) => {
var array: Project[] = [];
for (let entry of res.json().files) {
array.push(JsonConvert.deserializeObject(entry, Project));
}
return array;
})
}
如果我想將json映射到我的類,我得到一個錯誤,他們無法創建類新項目(),因爲在類項目中注入http服務
您會如何做到這一點?最佳做法是什麼?