2017-09-18 80 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); 
    } 
);