简体   繁体   English

类型可观察<object>不可分配给类型 Observable<div id="text_translate"><pre> GetUserDetailsById(Id): Observable<userdetails> { return this.httpclient.get(this.BaseURI + '/user/UserDetailsById/' + Id).map((response: Response) => <userdetails>response.json()) } 1. constructor(private httpclient: HttpClient) 2. export class userdetails { idUser: number; firstName: string; lastName: string; email: string; permissions: string; isActive: boolean; displayRole: number; }</pre><p> 删除时出错.map((response: Response) => response.json())</p><p> Error: Type Observable<Object> is not assignable to type Observable我不想使用.map((response: Response) => response.json()) 有什么建议吗? 上面的代码在与 this.http 示例一起使用时工作正常:</p><p> 返回 this.http.get(this.BaseURI + '/user/UserDetailsById/' + Id).map((response: Response) => response.json())</p><ul><li> 构造函数(私有 http:Http)</li></ul></div></object>

[英]Type Observable<Object> is not assignable to type Observable

GetUserDetailsById(Id): Observable<userdetails> {

    return this.httpclient.get(this.BaseURI + '/user/UserDetailsById/' + Id).map((response: Response) => <userdetails>response.json())
  }

1. constructor(private httpclient: HttpClient)

2. export class userdetails
{
  idUser: number;
  firstName: string;
  lastName: string;
  email: string;
  permissions: string;
  isActive: boolean;
  displayRole: number;
}

Getting error while removing.map((response: Response) => response.json())删除时出错.map((response: Response) => response.json())

Error: Type Observable<Object> is not assignable to type Observable I dont want to use.map((response: Response) => response.json()) Any suggestions? Error: Type Observable<Object> is not assignable to type Observable我不想使用.map((response: Response) => response.json()) 有什么建议吗? This above code is working fine when using with this.http example:上面的代码在与 this.http 示例一起使用时工作正常:

return this.http.get(this.BaseURI + '/user/UserDetailsById/' + Id).map((response: Response) => response.json())返回 this.http.get(this.BaseURI + '/user/UserDetailsById/' + Id).map((response: Response) => response.json())

  • constructor(private http: Http)构造函数(私有 http:Http)

This is the correct syntax:这是正确的语法:

getFromHttp(): Observable<ResponseAfterMap> {
return this.http.get<ResponseFromHttp>(this.BaseURI + '/user/UserDetailsById/' + Id).map((response: ResponseFromHttp) => response.json())
}

You need to define the generic type of the Observable.您需要定义 Observable 的通用类型。

In your case, simply add the Object type:在您的情况下,只需添加Object类型:

GetUserDetailsById(Id): Observable<Object> {
    /* ... */
}

But, if you know how the object is going to be, you might want to return some other type, like an interface for example.但是,如果您知道 object 将如何处理,您可能想要返回一些其他类型,例如接口。

interface myInterface {
    id: number;
    name: string
}

GetUserDetailsById(Id): Observable<myInterface> {
    /* ... */
}

If you are unsure of what the type is going to be, you can return any ( but this is not recommended ).如果您不确定类型是什么,您可以返回任何(但不推荐这样做)。

GetUserDetailsById(Id): Observable<any> {
    /* ... */
}

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

相关问题 Boo中Observable.Create的类型问题 - Type problem with Observable.Create from Boo 可观察对象的同步机制 - Synchronization mechanism for an observable object 参数类型“A”不能分配给“B”类型? - Argument type 'A' is not assignable to type 'B'? 参数类型不能分配给参数类型 - Argument type is not assignable to parameter type 可观察的CollectionViewSource - Observable CollectionViewSource Nullable DatabaseGeneratedOption不能分配给参数类型Nullable DatabaseGeneratedOption - Nullable DatabaseGeneratedOption is not assignable to parameter type Nullable DatabaseGeneratedOption Winform应用程序接口“参数类型不能分配给参数类型” - Winform application program interface “Argument type is not assignable to parameter type” 如果可观察集合中对象的属性发生更改,则在XAML中调用转换器 - call converter in XAML if property of a object in the observable collection changes 有没有办法让 XmlSerializer(或 DataContractSerializer)序列化可观察的 object 的属性? - Is there a way to get XmlSerializer (or DataContractSerializer) to serialise the property of an observable object? 用 Observable.Create 包裹 Observable.FromEventPattern - Wrap Observable.FromEventPattern with Observable.Create
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM