![](/img/trans.png)
[英]Resolving promise angular2: Property 'includes' does not exist on type '[]'
[英]Property 'includes' is missing in type 'Subscription' -angular2
我一直在尝试从节点到服务onInit获取对象的数组,并将其分配给组件。 我能够查看服务中的数据,但是当我尝试分配给组件中的变量时,出现以下错误。 我也包括了我的代码。 请对此表示赞同。我所需要的只是从服务中获取该数组,并将其分配给组件中的最终列表。
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();
}
subscribe
回报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);
}
您应该使用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>
用于严格键入
请参阅下面的答案。 它向您说明了如何从服务器,服务以及最终组件获取数据。 接收Jason而不是html 。 我正在使用mongodB,但您的后端可能有所不同。 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.