2017-09-18 4 views
0

Kann man mit dem neuen HttpClient in Angular (4.3+) auch eine Typüberprüfung auf POST (und Art anderer Anfragen) durchführen? Die offizielle Dokumentation (https://angular.io/guide/http#typechecking-the-response) erwähnt nur GET-Anfragen:Angular HttpClient Tychecking Beitrag Request's 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; 
}); 

Ist es die gleiche Art und Weise für andere Art von Anfragen arbeiten?

Antwort

2

Zu meinem Wissen, tut es. Zum Beispiel

interface LoginResponse { 
    token: string; 
} 

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