简体   繁体   中英

Is it bad practice to use a global navigation provider in ionic3 with lazy loading?

I am using a global provider to open pages by passing the navigation controller from the page to the provider method. Is there any other way to do this better?

Main objective is not to repeat my self. Open page method I use in the globalProvider.ts is as follows. currentActive page is stored in a variable of the provider in case user logins to the app he will be redirected to the last active page (currentActive)

globalProvider.ts

  openPage(navCtrl, page, params, setActive = true) {
    this.showLoading();
    if (setActive) {
      this.currentActive = { root: page, params: params };
    } else {
      this.currentActive = { root: 'HomePage', params: null };
    }
    navCtrl.setRoot(page, params).then(res => {
      if (!res) {
        this.showError('Please login to view page!');
        navCtrl.setRoot('LoginPage', {
          activePage: this.currentActive.root
        }).then(res => {
          this.hideLoading();
        }).catch(err => {
          this.hideLoading();
        });
      }
      this.hideLoading();

    }).catch(error => {
      this.hideLoading();
    });
  }

I don't think there is anything bad with this practice. I also do similar logic to avoid repeating the show loading and hide loading logic in every page.

However I also keep one more parameter , to help me decide if the page should be set as root or should be pushed on current stack.

This also helps me to track page views for analytics from a central place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM