2017-09-18 42 views
0

是否有可能在Angular(4.3+)中使用新的HttpClient类型检查POST(以及其他请求的类型)?官方文档(https://angular.io/guide/http#typechecking-the-response)只提到GET请求:Angular HttpClient Typechecking Post Request的Repsonse

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; 
}); 

是否对其他类型的请求的相同的方式工作?

回答

2

据我所知,它的作用。例如

interface LoginResponse { 
    token: string; 
} 

... 
this.http.post<LoginResponse>(url, body, options).subscribe(
    data => { 
     this.jwtoken = data.token; 
     return 'Login successful'; 
    }, 
    err => { 
     console.log(err); 
    } 
);