离子 2 无限滚动错误



我正在尝试在ionic2中进行无限滚动,但是每当页面加载时,我都会收到此错误Cannot read property 'complete' of undefined

.HTML

<ion-content>
<ion-list>
<ion-item *ngFor="let item of posts" (click)="passurl($event,item)">
<h2>{{item.title}}</h2>
</ion-item>
</ion-list>
<ion-infinite-scroll (ionInfinite)="ionViewDidLoad($event)">
<ion-infinite-scroll-content></ion-infinite-scroll-content>
</ion-infinite-scroll>
</ion-content>

.JS

ionViewDidLoad(infiniteScroll){
let loader = this.LoadingController.create({
content: 'Please Wait'
});
loader.present().then(()=>{
this.http.get('http://mynewweb/templates/messages/titles.php?church_id='+ this.church_id+'&msg_type='+this.NavParams.data.id+ "&page="+this.page).map(res => res.json()).subscribe(data =>{
console.log(JSON.stringify(data));
setTimeout(() => {
for(var i=0;i<data.length;i++){
this.numofposts = data.length;
this.posts.push(data[i]);
}
infiniteScroll.complete();
}, 500);
});
this.page=this.page+1;
loader.dismiss();
});
}

我从他们的网站上关注了有关 ionic2 框架的文档。 对此有任何帮助吗? 谢谢

你不能使用ionViewDidLoad钩子作为无限卷轴的目标。您可以将相同的逻辑放在不同的方法中,如下所示:

<ion-infinite-scroll *ngIf="!hideInfinite" (ionInfinite)="loadData($event)">
<ion-infinite-scroll-content></ion-infinite-scroll-content>
</ion-infinite-scroll>

在组件代码中:

public hideInfinite: boolean;
public loadData(infiniteScroll?: any): void {
let loader = this.LoadingController.create({
content: 'Please Wait'
});
loader.present().then(()=>{
this.http.get('http://mynewweb/templates/messages/titles.php?church_id='+ this.church_id+'&msg_type='+this.NavParams.data.id+ "&page="+this.page).map(res => res.json()).subscribe(
data => {
console.log(JSON.stringify(data));
if(!data || !data[0]) {
// Hide the infiniteScroll if there's no more data
this.hideInfinite = true;
}
setTimeout(() => {
for(var i=0;i<data.length;i++) {
this.numofposts = data.length;
this.posts.push(data[i]);
}
// Check if it's not null/undefined before calling the complete method
if(infiniteScroll) {
infiniteScroll.complete();
}
}, 500);
});
this.page=this.page+1;
loader.dismiss();
});
}

如果要在ionViewDidLoad生命周期挂钩中使用相同的逻辑,可以调用相同的方法:

ionViewDidLoad(){
// You can now call the method without sending anything, 
// since in the code we check if the infiniteScroll is defined 
// before using its methods
this.loadData();
}

旧的离子需要这样写

$event.state = "closed";

新版本离子需要这样写

infiniteScrollEvent.complete();

(或(

infiniteScrollEvent.target.complete();

ionViewDidLoad(infiniteScroll( {

this.http.get('http://mynewweb/templates/messages/titles.php?church_id=' + this.church_id + '&msg_type=' + this.NavParams.data.id + "&page=" + this.page).map(res => res.json()).subscribe(data => {
console.log(JSON.stringify(data));
if (infiniteScroll) {
infiniteScroll.complete();
}
});
this.page = this.page + 1;

}

public play=true;
public loadData(infiniteScroll?: any): void {
let loader = this.LoadingController.create({
content: 'Please Wait'
});
loader.present().then(()=>{
this.http.get('http://mynewweb/templates/messages/titles.php?church_id='+ this.church_id+'&msg_type='+this.NavParams.data.id+ "&page="+this.page).map(res => res.json()).subscribe(
data => {
this.numofposts=this.numofposts+data.length;
this.itemsdisplayed = this.itemsdisplayed + 10;
if(this.itemsdisplayed > this.numofposts) {
// Hide the infiniteScroll if there's no more data
this.play = false;
}
setTimeout(() => {
for(var i=0;i<data.length;i++) {
this.numofposts = data.length;
this.posts.push(data[i]);
}
// Check if it's not null/undefined before calling the complete method
if(infiniteScroll) {
infiniteScroll.complete();
}
}, 500);
});
this.page=this.page+1;
loader.dismiss();
});
}

.HTML

<ion-infinite-scroll *ngIf="play" (ionInfinite)="loadData($event)">
<ion-infinite-scroll-content></ion-infinite-scroll-content>
</ion-infinite-scroll>

最新更新