选项卡页面中的功能加载两次

时间:2017-04-27 05:59:44

标签: angular ionic2

我的工作页面中有一个功能,它是标签页面之一。我写了一个页面进入后从API请求数据时要加载的函数。

注意:除了标签页面之外,推开页面工作正常。

这在Ionic 3.0.1上工作得很好,但是当我进入页面时,我已经升级到Ionic 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,那就不能解决问题。

1 个答案:

答案 0 :(得分:0)

显然升级到Ionic 3.1.1将解决此问题。如果其他人遇到此问题并且您仍处于Ionic版本3.1.0,则只需升级到3.1.1+即可解决问题。

相关问题