【问题标题】:My page repeats same content even after scrolling down in pagination即使在分页中向下滚动后,我的页面也会重复相同的内容
【发布时间】:2019-06-27 00:11:54
【问题描述】:

我正在为我的新闻应用程序编写代码,但即使在向下滚动后我也得到相同的内容我希望在我向下滚动页面时加载接下来的 10 个内容

constructor(public navCtrl: NavController,toastCtrl: ToastController,public loadingCtrl: LoadingController, public api:Api, navParams: NavParams, items: Items,public http: HttpClient) {

let loader = this.loadingCtrl.create({
  content: "Please wait..."
});
loader.present().then(() => {
  this.api.getVideos(this.page+1, 10).subscribe(data => {
    console.log(data)
    this.getData = data
    loader.dismiss()
  }, err=>{
    console.log(err)
    loader.dismiss()
  })
}) }   doInfinite(infiniteScroll: any) {
       setTimeout(() => {
        console.log(this.page)
     this.api.getVideos(this.page + 1).subscribe(data => {
       this.page = this.page + 1
       console.log(data)
       // this.getData.push(data)
       this.getData = this.getData.concat(data);
       console.log(this.getData)
       infiniteScroll.complete()
     })
     }, 1000)
    }

这是我的 api 文件:

getVideos(page, perPage){ 
  return this.http.get(`${this.api_url}/postsfilter[post_format]=${page}&per_page=${perPage}`);

}

【问题讨论】:

    标签: ionic-framework ionic3


    【解决方案1】:

    移除 concat 并检查 this.page 值。

     page = 0; 
     getData: any = [];
    
     constructor(public navCtrl: NavController,
                    toastCtrl: ToastController,
                    public loadingCtrl: LoadingController, 
                    public api:Api, 
                    navParams: NavParams, 
                    items: Items,
                    public http: HttpClient) {
    
       loader.present().then(() => {
          this.api.getVideos(this.page, 10).subscribe(
            data => {
              console.log(data);
              this.getData = data;
              loader.dismiss();
            },
            err => {
              console.log(err);
              loader.dismiss();
            }
          );
        });            
    
             }   
    
    
      doInfinite(infiniteScroll: any) {
        setTimeout(() => {
          console.log(this.page);
          //you should define here where new value for page!!
          this.page = this.page + 1;
          this.api.getVideos(this.page, 10).subscribe(data => {
            console.log(data);
         //every scroll we are pushing new values to getData array.
    
            this.getData.push(data);
            // this.getData = this.getData.concat(data);
            console.log(this.getData);
            infiniteScroll.complete();
          });
        }, 1000);
      }
    

    【讨论】:

    • 我不知道什么数据新值是什么?
    • // this.getData.push(data);我们将它用于新的价值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多