1

我在使用新的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,有人可以帮忙吗?

4

2 回答 2

2

您可以.map在那里使用来转换/修改isValid标志。另外我建议您将User类更改为接口。

public loadAllUsers(): Observable<User[]> {
    return this.http
     .post<any>('/api/admin/getUsers/', 0)
     .map((res: any) =>
        return <User[]>res.map(item => {
           item.isValid = item.isValid === 'Y';
           item.isValidPassword = item.isValidPassword === 'Y';
           return item;
        });
     });
}
于 2017-12-12T16:15:39.477 回答
1

试试下面,这应该适用于你而不改变用户结构,你只需要将函数的返回类型修改为用户数组

   loadAllUsers(): Observable<User[]> {
    return this.http.get("apiURL")
        .map(res => {
          return res.json().results.map(item => {
            return new User(
                  item.userId,
                item.username,
                item.password,
                item.isValid === 'Y' ? true : false,
                item.type,
                item.isValidPassword === 'Y' ? true : false
            );
          });
        });
  }

由于此方法返回可覆盖的另一端使用loadAllUsers().subscribe(data=> this.userArray= data);- 如果它没有它就可以工作,或者如果它不起作用,请使用它


首先,您尝试做的是Get请求获取数据,您可以这样做

你的用户类将是

export class User{
    userId: number;
    username: string;
    password: string;
    isValid: string;
    type: string;
    isValidPassword: string;
    get isValidPasswordBool():boolean {
       return this.isValidPassword=='Y';
    }
    get isValidBool():boolean {
       return this.isValid=='Y';
    }
}

调用 http 服务,如下所示

import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions, 
             Response, ResponseContentType } from '@angular/http';

GetAllUser(): Observable<Array<User>> {
    let options = new RequestOptions({ headers: headers });
    return this._http.get("/api/admin/getUsers/",
                       options).subscribe(response => response.json());
}
于 2017-12-12T16:09:58.553 回答