简体   繁体   English

输入&#39;可观察的<HttpEvent<T> &gt;&#39; 不可分配到类型 &#39;Observable<T> &#39;

[英]Type 'Observable<HttpEvent<T>>' is not assignable to type 'Observable<T>'

The problem is at Line 14问题出在第 14 行

Type 'Observable<HttpEvent<T>>' is not assignable to type 'Observable<T>'.
  Type 'HttpEvent<T>' is not assignable to type 'T'.
    'HttpEvent<T>' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
      Type 'HttpSentEvent' is not assignable to type 'T'.
        'HttpSentEvent' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.ts(2322)

If I remove the second parameter, this.getHttpParams(obj) , then it works well.如果我删除第二个参数this.getHttpParams(obj) ,那么它运行良好。

But I need to pass the parameters.但我需要传递参数。

How to solve this?如何解决这个问题?

import { Injectable } from '@angular/core';
import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class DataService {
  protected url: string;

  constructor(private http: HttpClient) {}

  get<T>(endpoint: string, obj: object = null): Observable<T> {
    return this.http.get<T>(this.url + endpoint, this.getHttpParams(obj)); // Problem is here.
    // If I remove the second parameter: , this.getHttpParams(obj) - then it works good.
    // But I need to pass the parameters. How to solve this?
  }
  protected getHttpParams(obj: object) {
    const requestOptions: any = {};
    requestOptions.headers = new HttpHeaders({
      Accept: 'application/json',
      'Content-Type': 'application/json'
    });

    if (obj !== null) {
      requestOptions.params = this.objectToHttpParams(obj);
    }

    return requestOptions;
  }

  protected objectToHttpParams(obj: object): HttpParams {
    let params = new HttpParams();
    for (const key of Object.keys(obj)) {
      params = params.set(key, (obj[key] as unknown) as string);
    }

    return params;
  }
}

get has a lot of overloads, some that return Observable<T> and others that return Observable<HttpEvent<T>> . get有很多重载,一些返回Observable<T>和其他返回Observable<HttpEvent<T>> If the return value from getHttpParams is any , it thinks you'll get the latter.如果getHttpParams的返回值是any ,它认为你会得到后者。

The minimal fix is therefore to be more specific about what that method could return, for example:因此,最小的修复是更具体地说明该方法可以返回的内容,例如:

protected getHttpParams(obj: object): {headers: HttpHeaders, params?: HttpParams} { ... }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 输入&#39;可观察的<Object> &#39; 不可分配给类型 &#39;Observable<IUser[]> &#39; - Type 'Observable<Object>' is not assignable to type 'Observable<IUser[]>' 数字类型上的@observable不能创建可观察的 - @observable on numeric type doesn't create observable 可观察的 <void | AuthError> &#39;不可分配给&#39;Observable类型 <Action> - Observable<void | AuthError>' is not assignable to type 'Observable<Action> RXJS可观察的枚举类型,类型为“可观察” <Type> &#39;不可分配给&#39;Type&#39;类型 - RXJS Observable enum type, Type 'Observable<Type>' is not assignable to type 'Type' 不可分配给“可观察”类型 <task> &#39;。角4 - is not assignable to type 'Observable<task>'.Angular 4 将数组转换为可观察的类型<T[]> - Convert array to the type of Observable<T[]> 无法将“订阅”类型分配给“可观察”类型 <Exercise[]> “ - Type 'Subscription' is not assignable to type 'Observable<Exercise[]>' 类型“可观察” <boolean | “”> &#39;不可分配给&#39;Observable类型 <boolean> &#39;TS2322 - Type 'Observable<boolean | “”>' is not assignable to type 'Observable<boolean>' TS2322 输入&#39;可观察的<Response> &#39; 不可分配给类型 &#39;Observable <HttpResponse<Blob> &gt;&#39; - Type 'Observable<Response> ' is not assignable to type 'Observable<HttpResponse<Blob>>' 类型“ this”不可分配给类型“ T” - Type 'this' is not assignable to type 'T'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM