Use variable in multiple classes


#1

Good Morning
I have a variable (data.response) in my code that stores the return of a query in the database.
Now I want to use this variable in other classes to do other database queries from that class.
Can someone help me?

submit() {
let toast = this.toastCtrl.create({ duration: 3000, position: ‘bottom’ });
var link = ‘my_link_api’;
var data = JSON.stringify({username: this.username,
senha: this.senha});

    this.http.post(link, data)
    .subscribe(data => {
        this.data.response = data._body;
        if(this.data.response === 'Você não tem login')
        {
          toast.setMessage('O e-mail ou a senha digitada não é valido.');
        }
        else
        {
          this.navCtrl.push(HomePage);
        }
          
    }, error => {
        console.log("Oooops!");
    });
    toast.present();

}


#2

Hi,

You can store the variable in the local storage

Or create a provider.


#3

His tip was extremely helpful.
Thank you very much


#4

Nice, good to hear :slight_smile:


#5

If I may…

Does your variable have to exist every time the user stop/start your application? If so, yes it’s the right way to save it in a storage

Is your variable “only” a “session” variable (should only exists when your app run), then you should just save it in a provider