3
我在我的工作頁面中有一個功能,它是該頁面的一個頁面。我寫了一個函數,當頁面進入API請求數據時加載。在頁面加載兩次標籤頁中的函數
注意:除了選項卡的頁面,推送打開的頁面工作正常。
這是工作正常離子3.0.1,但是一旦我已升級到離子3.1.0當我進入頁面該函數加載兩次。
這是我的work.ts代碼
ionViewWillEnter() {
if(this.auth.authUser()) {
this.isLogin = true;
this.userData = this.auth.getUserInfo();
} else {
this.isLogin = false;
}
if(this.isLogin) {
this.getClientList(true);
} else {
this.clientData = [];
}
}
...
private getClientList(resetPage) {
console.log('loaded getClientList');
this.isEmpty = false;
this.userData = this.auth.getUserInfo();
if(resetPage) {
this.content.scrollToTop(0);
this.clientPage = 1;
}
let pageSkip = (this.clientPage-1)*this.clientPerPage;
let postData = 'take=' + this.clientPerPage;
postData += '&skip=' + pageSkip;
postData += '&uid=' + this.userData.userId;
if (this.category != '0') {
postData += '&category=' + this.category;
}
if (this.gradeId) {
postData += '&grade=' + this.gradeId;
}
let params = {
link: 'app/client/my_client_list',
data: postData,
}
this.showLoading('loading...');
this.api.httpApi(params).subscribe(data => {
this.loading.dismiss();
if (data.status_code == 1) {
this.clientData = [];
if(data.data.length > 0) {
this.clientData=data.data;
// this.productState = 'in';
if(this.clientPage < 1) {
this.clientPage++;
}
if(data.data.length < this.clientPerPage) {
this.isEnd = true;
} else {
this.isEnd = false;
}
} else {
this.isEmpty = true;
this.isEnd = false;
}
} else if (data.status_code == 500) {
this.navCtrl.push('LoginPage');
} else if (data.status_code == 0) {
this.isEmpty = true;
this.isEnd = false;
} else {
this.showError('network problem');
}
},
error => {
this.loading.dismiss();
this.pushLogin();
// this.showError(error);
});
}
控制檯日誌輸出
Hello AuthService Provider
work.ts:173 loaded getClientList
work.ts:173 loaded getClientList
離子信息
Cordova CLI: 6.5.0
Ionic Framework Version: 3.1.0
Ionic CLI Version: 2.2.1
Ionic App Lib Version: 2.2.0
Ionic App Scripts Version: 1.3.4
ios-deploy version: Not installed
ios-sim version: Not installed
OS: Windows 10
Node Version: v6.10.2
Xcode version: Not installed
[編輯]
有一種工作是將API請求函數放在構造函數中,然後函數將只運行一次。但是,如果你想使用IonViewWillEnter,那並不能解決問題。