我在使用新的HttpClient
.
User[]
之前,我可以使用将用户 Json 数组映射到reduce
,如下所示:
export class User {
constructor(
public userId: number,
public username: string,
public password: string,
public isValid: boolean,
public type: string,
public isValidPassword: boolean) {
}
}
public loadAllUsers() {
return this.http
.post('/api/admin/getUsers/', 0)
.map((res: any) => {
return new User(
res.userId,
res.username,
res.password,
res.isValid === 'Y',
res.type,
res.isValidPassword === 'Y'
);
})
.reduce((x, y) => { x.push(y); return x; }, <any[]>[]);
}
这是/api/admin/getUsers/
回应的一部分:
[
{
"userId": 1,
"username": "Ebraheem Alrabee",
"password": "827ccb0eea8a706c4c34a16891f84e7b",
"isValid": "Y",
"type": "admin",
"isValidPassword": "Y"
},
{
"userId": 4,
"username": "Sami",
"password": "827ccb0eea8a706c4c34a16891f84e7b",
"isValid": "Y",
"type": "user",
"isValidPassword": "Y"
}, ...
]
我试过这个:
public loadAllUsers() {
return this.http
.post<User[]>('/api/admin/getUsers/', 0);
}
但是我还是想用map
直接反序列化来代替User[]
,因为里面有我想改的值就好了isValid
。
我怎么能用 new 做同样的事情HttpClient
,有人可以帮忙吗?