我从Ionic和Angular开始,我面临着变量类型的问题。 我正在查询输出一系列元素的API。
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import {PostService } from '../_providers/post-service';
@Component({
selector: 'page-home',
templateUrl: 'home.html',
providers:[PostService]
})
export class HomePage {
public posts: any;
private start:number=0;
constructor(public navCtrl: NavController, public postService: PostService) {
this.getPost();
}
getPost(){
return new Promise(
resolve => {
this.postService.load(this.start)
.then(data => {
console.log(data);
for(let post of data) {
this.posts.push(post);
}
resolve(true);
});
});
}
doInfinite(infiniteScroll:any) {
console.log(this.start);
this.start+=10;
this.getPost().then(()=>{
infiniteScroll.complete();
});
}
likePost(post){
post.likes++;
}
}
第23行,我有这个问题: issue
并且console.log返回: console.log
最后,PostService类如下所示:
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class PostService {
perpage:number = 10;
constructor(public http: Http) {}
load(start:number=0) {
return new Promise(resolve => {
this.http.get('http://api.voyonsvoir.fr/?[limit]='+this.perpage+'&filter[skip]='+start)
.map(res => res.json())
.subscribe(data => {
resolve(data.posts);
});
});
}
}
感谢您的帮助。
答案 0 :(得分:1)
你需要在这里添加一些类型:
public posts: any[] = [];
然后从服务方法返回:
load(start:number=0): Promise<any[]> {
return new Promise<any[]>(resolve => {
注意:最好比any
更具体,但它应该至少让它运行。