Angular-HttpClient:将对象映射到数组属性
Angular-HttpClient: Map object to array properties
我正在调用一个 API,其中 returns 我是一个 JSON 对象。我需要将这个对象映射到一个数组。
下面是我的代码,但是在 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!')
})
);
}
也试过下面的代码,但它给我一个错误:
Type object is not assignable to type '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;
});
您不是return来自.map()
。你应该 return this.featureList
从服务
getFeatureListByLoggedInUser(userID: number) { debugger;
return this.http.get(this.myAppUrl + "api/Employee/GetMenusByUID/" + userID)
.pipe(
map(
(data: any[]) => {
debugger;
return this.featureList = data;
}), catchError(error => {
debugger;
return throwError('Something went wrong!')
})
);
}
编辑
此外,map
在您的代码中似乎是不必要的,因为您没有在里面操纵任何东西。您可以取消它并保留 catchError
来处理错误。
getFeatureListByLoggedInUser(userID: number) {
return this.http.get(this.myAppUrl + "api/Employee/GetMenusByUID/" + userID)
.pipe(
catchError(error => {
return throwError('Something went wrong!')
})
);
}
在你的组件中
this.service.getFeatureListByLoggedInUser(id)
.subscribe(data => { this.featureList = data })
我正在调用一个 API,其中 returns 我是一个 JSON 对象。我需要将这个对象映射到一个数组。
下面是我的代码,但是在 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!')
})
);
}
也试过下面的代码,但它给我一个错误:
Type object is not assignable to type '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;
});
您不是return来自.map()
。你应该 return this.featureList
从服务
getFeatureListByLoggedInUser(userID: number) { debugger;
return this.http.get(this.myAppUrl + "api/Employee/GetMenusByUID/" + userID)
.pipe(
map(
(data: any[]) => {
debugger;
return this.featureList = data;
}), catchError(error => {
debugger;
return throwError('Something went wrong!')
})
);
}
编辑
此外,map
在您的代码中似乎是不必要的,因为您没有在里面操纵任何东西。您可以取消它并保留 catchError
来处理错误。
getFeatureListByLoggedInUser(userID: number) {
return this.http.get(this.myAppUrl + "api/Employee/GetMenusByUID/" + userID)
.pipe(
catchError(error => {
return throwError('Something went wrong!')
})
);
}
在你的组件中
this.service.getFeatureListByLoggedInUser(id)
.subscribe(data => { this.featureList = data })