[英]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())
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.