簡體   English   中英

Angular HttpClient類型檢查發布請求的回復

[英]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);
    }
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM