gpt4 book ai didi

javascript - ionic 2 在 View 加载之前渲染数据

转载 作者:太空狗 更新时间:2023-10-29 17:33:32 26 4
gpt4 key购买 nike

我正在学习 ionic 2,目前正面临一些 ionic 生命周期问题。我想在api获取成功数据后显示数据。

import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';
import {LoadingController} from 'ionic-angular';

import WooCommerceAPI from 'woocommerce-api';

@Component({
selector: 'page-category',
templateUrl: 'category.html',
})
export class CategoryPage {

information: any[] = [];
infoChild: any[] = [];
wooApi: any;
wooCat: any;
categories: any = [];

constructor(public navCtrl: NavController, public loadingCtrl: LoadingController) {
this.wooApi = WooCommerceAPI(
{
url: 'abc.com/api/v1/cat',

}
);
//here also tried to call api

}


ngOnInit() {
//here also tried to call api

}
ionViewDidLoad() {
console.log("calling api");
/*from with nginit*/
this.wooApi.getAsync('products/categories?parent=0').then((data) => {
console.log("success");
this.wooCat = JSON.parse(data.body);
//this.information = this.wooCat;
for (let i = 0; i < this.wooCat.length; i++) {
console.log("in for loop");
this.wooApi.getAsync('products/categories?parent=' + this.wooCat[i].id).then((data) => {
console.log("get success", i);
let wooChild = JSON.parse(data.body);
for (let x = 0; x < wooChild.length; x++) {
this.infoChild.push(wooChild[x]['name']);
}
//console.log(this.infoChild)
this.information.push({
'items': {
'name': this.wooCat[i]['name'],
'children': [{
'name': this.infoChild
}]
}
});
this.infoChild = [];
});

}
console.log("current data", this.information);

});

}

}

我尝试使用 constructor() 调用 api,也尝试使用 **ngOnInit() 和 ionViewDidLoad() ** 数据正常但没有反射(reflect)在我的 View /html 页面上。

我的Html代码如下:

<accordion *ngFor="let catParent of information" [title]="catParent.items.name">
<!-- (click)="itemSelected(item)" -->
<ion-list *ngFor="let catChild of catParent.items.children">
<button ion-item *ngFor="let catChildName of catChild.name">
{{ catChildName }}
</button>
</ion-list>
</accordion>

因此,当我单击类别选项卡时,api 正在调用并获得响应但无法在我的 View 页面上看到此结果,现在如果我单击任何其他选项卡并再次单击类别选项卡,那么我可以看到此数据.

因为我是 ionic 2 的新手,所以我无法解决这个问题。谁能帮我解决这个问题。

提前致谢:)

最佳答案

  1. 最好从服务类或提供者类中触发 HTTP 请求
  2. IonViewDidLoad仅在加载页面时运行,因此将在触发 HTTP 请求之前加载空页面。
  3. 使用IonViewCanEnter可以在 View 呈现之前运行
  4. 使用 ionic loading component 显示加载这样用户就会知道后台发生了什么

关于javascript - ionic 2 在 View 加载之前渲染数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47236620/

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