大家好,我正在嘗試將來自The Muse API的一些 API 回應映射到我這樣創建的 APIResponseInterface:
發送請求的服務:
getJobs(page: number): Observable<APIResponseInterface>{
return this.http.get<APIResponseInterface>(this.url page)
}
API 回應介面
export interface APIResponseInterface {
page?: number,
page_count?: number,
items_per_page?: number,
took?: number,
timed_out?: boolean,
total?: number,
results: Array<JobInterface>
}
收到“結果”的專案后應該映射到這個界面:
export interface JobInterface {
contents: HTMLAllCollection,
name: string,
type?: string,
publication_date?: Date,
short_name?: string,
model_type?: string;
id? : number,
locations: LocationInterface[]
level: LevelsInterface[],
company: CompanyInterface,
}
在我的 Angular 組件中,我使用此函式將結果映射到 JobInterface 陣列:
this.jobsService.getJobs(1)
.subscribe((response) => {
this.jobs = response.results.map<JobInterface>(job => {{
name: job.name
}}
)
console.log(this.jobs)
})
但我收到如下錯誤:
TS2345: Argument of type '(job: JobInterface) => void' is not assignable to parameter of type '(value: JobInterface, index: number, array: JobInterface[]) => JobInterface'.
你能幫我弄清楚我做錯了什么嗎?這樣做的正確方法是什么?
uj5u.com熱心網友回復:
您需要用括號而不是花括號來包裝回傳
this.jobs = response.results.map<JobInterface>(job => ({
name: job.name
...
})
當你使用花括號時,你需要添加一個 return 陳述句。
this.jobs = response.results.map<JobInterface>(job => {
return {
name: job.name
...
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/513295.html
標籤:有角度的打字稿网络
