“订阅"类型中缺少属性“包含"-angular2 [英] Property 'includes' is missing in type 'Subscription' -angular2

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

问题描述

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

错误在C:/Users/girija/Documents/animapp/src/app/components/list.component.ts (28,5):类型订阅"不可分配给类型任何[]".订阅"类型中缺少属性包含".

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

 公开决赛名单=[]获取列表(){返回 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 返回 Subscription 对象,这不是你要找的.

尝试这样的事情:

app.service.ts

getList(): Observable{return 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);
}

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

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