2017-08-24 5 views
1

HTTP応答の種類を設定することができません私は、HttpClient要求を使用して、いくつかの機能を公開するサービスを持っているそのうちの一つは、postさ:アンギュラ - 適切

post<T>(url: string, data: any, params?: {[key: string]: any}): Observable<HttpEvent<T>> { 
    return this.http.post<T>(this.host + url, data, this.getRequestOptions(params)); 
} 

ここで重要なのは、戻り署名であるObservable<HttpEvent<T>> 。私は、関数を呼び出すとき

は、その後、私は二つの問題を得る:

最初の問題は、私が言って原因リントエラーに <string>とタイプして渡すことができないということです
this.api.post<string>('/tokens', credentials).subscribe((token: string) => {}); 

Expected 0 type arguments but got 1 

2番目の問題は、応答のすべてのデータが、私が送信したものではなく、タイプHttpEvent<T>になります。この例では、タイプがstringである必要があります。例中のSO

私はこのエラーを取得する上:

Argument of type '(token: string) => void' is not assignable to parameter of type '(value: HttpEvent<{}>) => void'. Types of parameters 'token' and 'value' are incompatible. Type 'HttpEvent<{}>' is not assignable to type 'string'. Type 'HttpProgressEvent' is not assignable to type 'string'.

私はきちんと私の応答タイプを設定することができるように私はこの問題を解決するにはどうすればよいですか?

答えて

1

これを試してみてください:

post<T>(url: string, data: any, params?: {[key: string]: any}): Observable<T> { 
      return this.http.post<T>(this.host + url, data, this.getRequestOptions(params)).map((res: T) => res); 
     } 
+0

が、残念ながら動作しません。 – Chrillewoodz

+0

私はこれがうまくいくと思っていますが、地図はかなり無駄です:D –

+0

https://plnkr.co/edit/cSJnCp8UtKv7piG1d0yi?p=preview –