从来自 Angular 中的 RESTful 服务的大数据中获取特定字段的最佳做法是什么?
What's the best practice to fetch specific fields from big data coming from RESTful service in Angular?
我正在调用 RESTful 服务,其中 return 数据包含很多字段,但我想使接口像 DTO 一样只携带我需要的数据。
我使用了 rxjs 的管道和地图,但我不知道这是否是最佳实践:
平时return编辑的数据:
[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
},
{...}
]
我只需要的数据:
[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz"
},
{...}
]
用户界面:
export interface User {
id?:number,
name?:string,
email?:string,
username?:string
}
用户服务:
export class UsersService {
constructor(private http: HttpClient) {}
getAllUsers(): Observable<User[]> {
return this.http.get<User[]>("https://jsonplaceholder.typicode.com/users")
.pipe(
map((data: User[]) =>
data.map((item: User) => {
return {
id: item.id,
name: item.name,
email: item.email,
username: item.username,
};
})
)
);
}
}
我找到了答案:
用户界面:
interface User {
id: number,
name: string,
email: string,
username: string
}
interface UserResponse {
id: number;
name: string;
username: string;
email: string;
address: any; // lazily using 'any' here
phone: string;
website: string;
company: any; // lazily using 'any' here
}
用户服务:
export class MyService {
constructor(private http: HttpClient) { }
getData(): Observable<User[]> {
return this.http.get<UserResponse[]>('https://jsonplaceholder.typicode.com/users')
.pipe(
map((data: UserResponse[]) =>
data.map((item: UserResponse) => {
return <User>{
id: item.id,
name: item.name,
email: item.email,
username: item.username,
};
})
)
);
}
}
我正在调用 RESTful 服务,其中 return 数据包含很多字段,但我想使接口像 DTO 一样只携带我需要的数据。 我使用了 rxjs 的管道和地图,但我不知道这是否是最佳实践:
平时return编辑的数据:
[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
},
{...}
]
我只需要的数据:
[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz"
},
{...}
]
用户界面:
export interface User {
id?:number,
name?:string,
email?:string,
username?:string
}
用户服务:
export class UsersService {
constructor(private http: HttpClient) {}
getAllUsers(): Observable<User[]> {
return this.http.get<User[]>("https://jsonplaceholder.typicode.com/users")
.pipe(
map((data: User[]) =>
data.map((item: User) => {
return {
id: item.id,
name: item.name,
email: item.email,
username: item.username,
};
})
)
);
}
}
我找到了答案:
用户界面:
interface User {
id: number,
name: string,
email: string,
username: string
}
interface UserResponse {
id: number;
name: string;
username: string;
email: string;
address: any; // lazily using 'any' here
phone: string;
website: string;
company: any; // lazily using 'any' here
}
用户服务:
export class MyService {
constructor(private http: HttpClient) { }
getData(): Observable<User[]> {
return this.http.get<UserResponse[]>('https://jsonplaceholder.typicode.com/users')
.pipe(
map((data: UserResponse[]) =>
data.map((item: UserResponse) => {
return <User>{
id: item.id,
name: item.name,
email: item.email,
username: item.username,
};
})
)
);
}
}