Static get parameters() because navController in Service Provider

Hi, my name is Luan and I have a problem.

I’ve added the NavController in my Service Provider, because I want set Root to Login Page depending of API return.

So, I added import { NavController, App } from 'ionic-angular';, private app: App in constructo and this.nav = app.getActiveNav();.

But now, I need to add static get parameters() { return [[NavController], ..., ... [ApiProvider]]; } in all Pages.

Exists any solution?

In my opinion, this is a design mistake. Services should not be dealing with the view layer. Expose an event emitter or an observable in the service, subscribe to it in a component controller, and set the root page appropriately there.

1 Like

I agree with you, is a design mistake. But, just because I started with Ionic in less than three week.
You can not imagine how much I took to understand the Promises, hahaha.

I’ll find out about events and how to call them when I need to.

Thanks.

[Edit]
Found this: https://forum.ionicframework.com/t/best-way-to-check-login-globally-in-ionic-2/49813

It’s works. Thanks!

Added to app.ts:

    import { Events } from 'ionic-angular';

    constructor(private events: Events){ }

    this.events.subscribe('user:logout', () => {
      window.localStorage.clear();
      this.nav.setRoot(LoginPage).then(() => {
        this.alert.create({
          title: 'Ops!',
          subTitle: 'Please login.',
          buttons: ['OK']
        }).present();
      });
    });

And in Service Provider:

    import { Events } from 'ionic-angular';

    constructor(private events: Events){ }

    if(!this.isLogged){
      this.events.publish('user:logout');
    }
1 Like