Promises in ionic

1

I am practicing ionic to clarify concepts. In this case the promises.

I have an app with 2 pages login and home.

I want a user to validate the login, if it exists, redirect me to the home page. Otherwise stay in the form on the same page.

This validation I am doing in the ionViewWillEnter method of the login page, where I look for user data in SQLite.

The problem is that the validation is not done, the rest of the code is still running before the promise ends. I hope to give me to understand.

When I start the app, this does not work. But if I enter data in the login and go to home, and then return to the login, this works.

I think the problem is the so-called asynchronic of the promise, but how to control it so that it ends and only then the rest is still being executed.

Thank you very much already.

Code example of how I am posing:

public mensaje: string;

ionViewWillEnter(){

    this.dbServiceProvider.getUser().then(
        (data) => {
            this.mensaje = "Ejecuto";
        },
        (err) =>{
            this.mensaje = "Error";
        }
    ).catch(
        (ex) => {
            this.mensaje = "Excepcion";
    });
}

Test code, when you start the app (in the login screen), the text is not displayed ... after login login to Home, back again to the login screen and then if the text is displayed. Why?

    
asked by Alejandro 23.11.2017 в 15:47
source

1 answer

0

I literally translate from a GitHub report, which has the same problem:

  

This is to be expected. Since all these events are related to the   nav-controller, there needs to be a parent nav-controller. Since   you're loading to root component, there is no parent nav-controller, so   no events fire.

     

If you need to catch event when that root component is rendered, you   can use the angular lifecycle hooks. Or, if you need to subscribe to   When child components are loaded, there are observables you can use.    link

TRANSLATION

  

This behavior is as expected. Since all these events   are related to the nav-controller, there needs to be a   parent nav-controller. Since you are loading a root component, do not   there is a parent nav-controller, so no event is triggered.

     

If you need to capture events when that root component is rendered,   you can use Angular's life cycles (ngOnInit in this case). OR   If you need to subscribe to when a child component is loaded, there is   Observed that you can use.    link

    
answered by 23.11.2017 / 18:21
source