Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将响应映射到模型对象_Javascript_Angular_Typescript_Angular Httpclient - Fatal编程技术网

Javascript 将响应映射到模型对象

Javascript 将响应映射到模型对象,javascript,angular,typescript,angular-httpclient,Javascript,Angular,Typescript,Angular Httpclient,我正在尝试将GET调用的响应映射到我的对象,该对象具有JSON对象的属性1:1。这是我的密码: 线程模型: export class Thread { private source: Source; private target: Target; private messages: Message[]; constructor(data) { this.source = data.source; this.target = data.target; th

我正在尝试将GET调用的响应映射到我的对象,该对象具有JSON对象的属性1:1。这是我的密码:

线程模型:

export class Thread {
  private source: Source;
  private target: Target;
  private messages: Message[];

  constructor(data) {
    this.source = data.source;
    this.target = data.target;
    this.messages = data.messages;
  }

  sum() {
    return 'Hello from Thread';
  }
}
线程服务:

@Injectable()
export class ThreadService {
  constructor(private http: HttpClient) {
  }
  getAll(): Observable<Thread[]> {
    return this.http.get<Thread[]>('/api/thread').map(value => new Thread(value))
  }
}
@Injectable()
导出类线程服务{
构造函数(专用http:HttpClient){
}
getAll():可观察{
返回这个.http.get('/api/thread').map(值=>newthread(值))
}
}
ThreadService中的getAll方法给了我以下错误:

src/app/service/thread.service.ts(13,5):错误TS2322:Type “
可观察的
”不可分配给类型“
可观察的
”。 类型“
线程”
”不能分配给类型“
线程[]
”。 类型“
Thread
”中缺少属性“includes”

您能告诉我将响应映射到模型的正确方法是什么吗?该模型允许我调用该模型中的自定义方法?我读了一些关于拦截器的文章,但是我找不到任何例子。谢谢你的帮助。

@Injectable()
@Injectable()
export class ThreadService {
  constructor(private http: HttpClient) {
  }
  getAll(): Observable<Thread[]> {
    return this.http.get<Thread[]>('/api/thread').map(threads=> threads.map(thread => new Thread(thread)))
  }
}
导出类线程服务{ 构造函数(专用http:HttpClient){ } getAll():可观察{ 返回这个.http.get('/api/thread').map(threads=>threads.map(thread=>newthread(thread))) } }
您已经说过,
get
将返回一个
Thread[]
,整个方法也将返回一个
Thread[]
,但是
可观察的.map
将数组传递到
Thread
构造函数中,因此只返回
Thread
,而不是
Thread[]
。您是否打算使用
数组.map