【发布时间】:2018-12-14 17:09:33
【问题描述】:
我正在调用一个返回 JSON 对象的 API。我需要将此对象映射到一个数组。
以下是我的代码,但 API 调用后既没有收到任何响应也没有任何错误。
export class Features {
MenuId: number;
MenuName: string;
Description: string;
RoutePath: string;
}
featureList: Features[] = [];
constructor(private http: HttpClient)
getFeatureListByLoggedInUser(userID: number) { debugger;
return this.http.get(this.myAppUrl + "api/Employee/GetMenusByUID/" + userID)
.pipe(
map(
(data: any[]) => {
debugger;
this.featureList = data;
//return true;
}), catchError(error => {
debugger;
return throwError('Something went wrong!')
})
);
}
也试过下面的代码,但它给了我一个错误:
类型对象不可分配给类型'any[]'
featureList: Array<any> = [];
getFeatureListByLoggedInUser(userID: number) { debugger;
return this.http.get(this.myAppUrl + "api/Employee/GetMenusByUID/" + userID)
.subscribe(
data => {
debugger;
this.featureList = data;
});
}
编辑:
return this.http.get<Array<Features>>(this.myAppUrl + "api/Employee/GetMenusByUID/" + userID)
.subscribe(
data => {
debugger;
//this.featureList = data;
});
【问题讨论】:
标签: angular rxjs angular6 angular-httpclient rxjs6