属性 'includes' 在类型 'Subscription' -angular2 中缺失

Property 'includes' is missing in type 'Subscription' -angular2

我一直在尝试从节点获取对象数组到服务 onInit 并将其分配给组件。我能够查看服务中的数据,但是当我尝试分配给组件中的变量时,出现以下错误。我也包含了我的代码。请支持 this.All 我需要的只是从服务中获取该数组并将其分配给组件中的最终列表。

ERROR in 

    C:/Users/girija/Documents/animapp/src/app/components/list.component.ts (28,5): Type 'Subscription' is not assignable to type 'any[]'.
      Property 'includes' is missing in type 'Subscription'.

我的代码如下: app.service.ts:

   public finallist=[]
     getList(){
          return this.http.get('/api/list').subscribe(data=>{
             this.finallist=JSON.parse(data['_body']);
             return this.finallist;
        })
        }

app.component.ts:

  totallist=[];
     ngOnInit() {
        this.totallist=this.someService.getList();

      }

您应该使用 map 运算符将响应转换为 json 类型

getList(){
          return this.http.get('/api/list')
             .map(response=> <Type>response.json()) /////////
             .subscribe(data=>{
             this.finallist=JSON.parse(data['_body']);
             return this.finallist;
        })
        }

注:<Type>用于严格类型化

请看下面我的回答。它向您解释了如何从服务器获取数据,到服务,最后到组件。 Receiving Jason instead of html。我正在使用 mongodB 但您的后端可能不同。希望这有帮助。

subscribe returns Subscription对象,这不是你要找的。

尝试这样的事情:

app.service.ts

getList(): Observable<any> {
  return this.http.get('/api/list').map(data => data['_body']);
}

app.component.ts

totallist=[];

ngOnInit() {
  this.someService.getList().subscribe(data => this.totallist = data);
}