gpt4 book ai didi

javascript - ionic 2无限滚动错误

转载 作者:搜寻专家 更新时间:2023-10-30 22:00:18 27 4
gpt4 key购买 nike

我正在尝试在 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 Hook 作为 InfiniteScroll 的目标.您可以将相同的逻辑放在不同的方法中,如下所示:

<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 生命周期 Hook 中使用相同的逻辑,你可以调用相同的方法:

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

关于javascript - ionic 2无限滚动错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44891677/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com