Angular HttpClient 类型检查 Post 请求的响应
Angular HttpClient Typechecking Post Request's Repsonse
是否也可以使用 Angular (4.3+) 中的新 HttpClient 对 POST(以及其他请求的类型)进行类型检查?官方文档(https://angular.io/guide/http#typechecking-the-response)只提到GET请求:
interface ItemsResponse {
results: string[];
}
...
http.get<ItemsResponse>('/api/items').subscribe(data => {
// data is now an instance of type ItemsResponse, so you can do this:
this.results = data.results;
});
它是否适用于其他类型的请求?
据我所知,确实如此。例如
interface LoginResponse {
token: string;
}
...
this.http.post<LoginResponse>(url, body, options).subscribe(
data => {
this.jwtoken = data.token;
return 'Login successful';
},
err => {
console.log(err);
}
);
是否也可以使用 Angular (4.3+) 中的新 HttpClient 对 POST(以及其他请求的类型)进行类型检查?官方文档(https://angular.io/guide/http#typechecking-the-response)只提到GET请求:
interface ItemsResponse {
results: string[];
}
...
http.get<ItemsResponse>('/api/items').subscribe(data => {
// data is now an instance of type ItemsResponse, so you can do this:
this.results = data.results;
});
它是否适用于其他类型的请求?
据我所知,确实如此。例如
interface LoginResponse {
token: string;
}
...
this.http.post<LoginResponse>(url, body, options).subscribe(
data => {
this.jwtoken = data.token;
return 'Login successful';
},
err => {
console.log(err);
}
);