'Subscription'-angular2类型中缺少属性'includes' [英] Property 'includes' is missing in type 'Subscription' -angular2

查看:105
本文介绍了'Subscription'-angular2类型中缺少属性'includes'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  ERROR in 

C:/Users/girija/Documents/animapp/src/app/components/list.component.ts(28,5):类型'订阅'不能分配给'any []' 。
订阅类型中缺少'包含'属性。

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

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

app.component.ts:

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

}


解决方案

subscribe 返回订阅对象,这不是您所寻找的。

尝试这样的事情:



app.service.ts

  getList():Observable< any> {
返回this.http.get('/ api / list')。map(data => data ['_ body']);
}

app.component.ts

  totallist = []; 

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


I have been trying to get an array of objects from node to a service onInit and assign it to component. I am able to view data in service but when i try to assign to a variable in component, I get below error. I have included my code as well. Please favour on this.All i need is to just take that array from service and assign it to finallist in component.

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'.

My code is below: 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();

      }

解决方案

subscribe returns Subscription object, this is not what you have looking for.

Try something like this:

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);
}

这篇关于'Subscription'-angular2类型中缺少属性'includes'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆