Ionic 2 - How to store global variable?
<p><strong>THE SITUATION:</strong></p>

<p>In my app.component there is the login function. It is working properly and I get the user data from the API.</p>

<p>I need to store this data as a global variable to be able to access it throughout the app.</p>

<p>I thought that sharing a service was the way to do it, but unfortunately is not working as I thought.</p>

<p><strong>THE CODE</strong>:</p>

<p><strong>The service:</strong></p>

<pre><code>import { Injectable } from '@angular/core';

@Injectable()
export class LoginService {

  public accountInfo;

  setUserData(value) 
  {
    this.accountInfo = value;
  }

  getUserData() {
    return this.accountInfo;
  }

}
</code></pre>

<p><strong>The app.component:</strong></p>

<pre><code>loginSubmit()
{
    // Function reduced to what is essential to the question

    this.userService.submitLogin(email, password)
        .subscribe((response) =&gt; {

            if(response.result == 1) 
            {
                this.loginService.setUserData(response.account_info);

                var justTesting = this.loginService.getUserData();

                // Getting the proper result back
                console.log(justTesting);
            }

        }, (error) =&gt; {
            console.log(error);
        });
}
</code></pre>

<p>Trying to access the user data from another component:</p>

<pre><code>this.accountInfo = this.loginService.getUserData();
console.log(this.accountInfo);
</code></pre>

<p>The result is <code>undefined</code>. Probably because this.accountInfo (in the service) is instantiated again when the service is called from the other component...</p>

<p><strong>THE QUESTION</strong>:</p>

<p>How can I store some data as a global variable? Can it be done by sharing a service?</p>

<p>Thanks!</p>