【问题标题】:Ionic Angular nbOnInit Undefined?离子角 nbOnInit 未定义?
【发布时间】:2020-09-13 05:18:52
【问题描述】:

我对 Ionic 有疑问。我想在 ngOnInit 的函数中放入页数,但写的是 undefined

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { HttpConfigService } from '../services/http-config.service';
import { GeolocationService } from '../services/geolocation.service';
@Component({
  selector: 'app-commerces',
  templateUrl: './commerces.page.html',
  styleUrls: ['./commerces.page.scss'],
})
export class CommercesPage implements OnInit {

  url: string;
  itemListData = [];
  page_number = 1;
  page_limit = 8;
  isFinish = false;
  nbPages;

  constructor(
    private httpConfigService: HttpConfigService,
    private httpClient: HttpClient,
    private geolocationService: GeolocationService
  ) {
  }

  ngOnInit() {
    this.nbPages();
    this.getCommerces(true, "");
  }

  doInfinite(event) {
    this.nbPages();
    this.getCommerces(true, event);
  }

  sort() {
    this.itemListData.sort((a, b) => {
      if (a.title.rendered > b.title.rendered) {
        return 1;
      } else if (a.title.rendered < b.title.rendered) {
        return -1;
      } else {
        return 0;
      }
    });
  }

  onSearchChange(event) {
    console.log(event.detail.value);
  }


  npPage() {
    this.httpClient
      .get('https://exemple.fr/wp-json/wp/v2/listing', { observe: 'response' })
      .subscribe((data: any) => {
        // Here, resp is of type HttpResponse<MyJsonData>.
        // You can inspect its headers:
        this.nbPages = data.headers.get('X-WP-TotalPages');
      });
  }

  getCommerces(isFirstLoad, event) {
    for (let j = 1; j < this.nbPages; j++) {
      this.httpClient
        .get('https://exemple.fr/wp-json/wp/v2/listing?page=' + j, { observe: 'response' })
        .subscribe((data: any) => {
          for (let i = 0; i < data.body.length; i++) {
            data.body[i].location = this.geolocationService.getDistance(data.body[i]._geolocation_lat, data.body[i]._geolocation_long).toFixed(2);;
            data.body[i]._gallery = data.body[i]._gallery[Object.keys(data.body[i]._gallery)[0]]
            this.itemListData.push(data.body[i]);
          }

          if (isFirstLoad)
            event.target.complete();

        }, error => {
          console.log(error);
        })
    }
    this.sort();
    console.log(this.itemListData);
  }
}

nbPages 未定义,我需要此变量在 getCommerces() 中执行循环

您能帮我找出问题所在吗,Angular 对我来说是新的,在 ngOnInit 中无法更改变量的值?

【问题讨论】:

    标签: angular typescript ionic-framework


    【解决方案1】:

    你需要在你的类定义上实现 OnInit:

    export class YOURCLASSNAME implements OnInit {
    

    你正在调用一个函数: nbPages()

    但您的函数定义为:(没有最后的 s)

    npPage() {
    

    【讨论】:

    • 由于缺少某些元素,您需要共享更多代码
    • ERROR Error: Uncaught (in promise): TypeError: this.nbPages is not a function TypeError: this.nbPages is not a function 现在是我的错误
    • 阅读我的回复:您必须调用“npPage()”而不是 nbPages
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-08
    • 1970-01-01
    相关资源
    最近更新 更多