【发布时间】: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