如何为 Angular 应用程序组织 类?
How to organize classes for an Angular application?
在 TypeScript、JavaScript、Java 或其他任何语言中使用 classes 处理 API 响应的最专业方法是什么?
例如,一个应用程序需要三个资源:
- 账户(API:/account/:id)
- 汽车(API:/account/:id/cars/[:id])
- Driver (API: /account/:id/cars/:id/drivers)
每个账号有一辆或多辆汽车,每个汽车有一辆或多辆Driver。
我看到两种在我的应用程序中处理这些资源的方法:
为每个资源(帐户、汽车、Driver)创建一个 class 并嵌套它。例如。
class Account {
constructor(
public id: number,
public name: string,
public age: number
public cars: Car[]
) {}
}
class Car {
constructor(
public id: number,
public type: string,
public seats: number
public drivers: Driver[]
) {}
}
在这种情况下,API 会 return 一个已经嵌套的 JSON 响应,并且只需要请求一次数据。
为每个资源创建一个class,但不要嵌套它。首先仅获取帐户数据(调用 /account/1234),然后在应用程序生命周期的后期获取需要的司机和汽车。
class Account {
constructor(
public id: number,
public name: string,
public age: number
) {}
}
class Car {
constructor(
public id: number,
public type: string,
public seats: number
) {}
}
在这种情况下,对于每个 API,API 将 return 仅调用主要资源数据。
因此,资源应该始终嵌套在整个应用程序中并仅在一次调用,还是资源 class 应该存在并在应用程序中相互独立地重新加载?专业程序员使用哪种方式?
来自 Angular 风格指南
Consider using an interface for data models
Angular 4.3 引入了更简单的新方法来处理 http
请求 HttpClient
库
You can tell HttpClient
the type of the response to make consuming
the output easier and more obvious.
All you need to do is define an interface for the shape of the response and type-check against that interface:
interface Post {
title: string;
body: string;
};
// ...
constructor(private http: HttpClient) {}
getData() {
this.http.get<Post>(this.url).subscribe(res => {
this.postTitle = res.title;
});
}
So should resources always be nested over the complete application and
called up only at one time, or should the resource classes exist and
be reloaded independently of each other in the application? Which way is used by professional programmers?
这完全取决于您的使用情况
在 TypeScript、JavaScript、Java 或其他任何语言中使用 classes 处理 API 响应的最专业方法是什么?
例如,一个应用程序需要三个资源:
- 账户(API:/account/:id)
- 汽车(API:/account/:id/cars/[:id])
- Driver (API: /account/:id/cars/:id/drivers)
每个账号有一辆或多辆汽车,每个汽车有一辆或多辆Driver。
我看到两种在我的应用程序中处理这些资源的方法:
为每个资源(帐户、汽车、Driver)创建一个 class 并嵌套它。例如。
class Account { constructor( public id: number, public name: string, public age: number public cars: Car[] ) {} } class Car { constructor( public id: number, public type: string, public seats: number public drivers: Driver[] ) {} }
在这种情况下,API 会 return 一个已经嵌套的 JSON 响应,并且只需要请求一次数据。
为每个资源创建一个class,但不要嵌套它。首先仅获取帐户数据(调用 /account/1234),然后在应用程序生命周期的后期获取需要的司机和汽车。
class Account { constructor( public id: number, public name: string, public age: number ) {} } class Car { constructor( public id: number, public type: string, public seats: number ) {} }
在这种情况下,对于每个 API,API 将 return 仅调用主要资源数据。
因此,资源应该始终嵌套在整个应用程序中并仅在一次调用,还是资源 class 应该存在并在应用程序中相互独立地重新加载?专业程序员使用哪种方式?
来自 Angular 风格指南
Consider using an interface for data models
Angular 4.3 引入了更简单的新方法来处理 http
请求 HttpClient
库
You can tell
HttpClient
the type of the response to make consuming the output easier and more obvious. All you need to do is define an interface for the shape of the response and type-check against that interface:
interface Post {
title: string;
body: string;
};
// ...
constructor(private http: HttpClient) {}
getData() {
this.http.get<Post>(this.url).subscribe(res => {
this.postTitle = res.title;
});
}
So should resources always be nested over the complete application and called up only at one time, or should the resource classes exist and be reloaded independently of each other in the application? Which way is used by professional programmers?
这完全取决于您的使用情况