简体   繁体   中英

Calling variable within Ionic constructor returns UNDEFINED

How can I call a variable in a constructor?

constructor ( private auth: AngularFireAuth ) {
this.auth.authState.subscribe((user)=> {this.email = user.email});

this.url = 'http://localhost/check.php' + this.email;  
// NOW CALLIG this.email RETURN UNDEFINED. HOW CAN I MAKE IT WORK?
..........
..........
}

For an unavoidable reason I cannot add 'http://localhost/check.php' + this.email; after {this.email = user.email;

也许您可以尝试在Nav Controller中使用生命周期方法,该方法可以在页面事件的不同阶段触发。

You could also do the call in the subscriber. Or call a function from inside than you know your variables are set.

constructor ( private auth: AngularFireAuth ) {
this.auth.authState.subscribe((user)=> {
this.email = user.email;
this.url = 'http://localhost/check.php' + this.email; 
// NOW CALLIG this.email
})
}

// edit

constructor ( private auth: AngularFireAuth ) {
    this.auth.authState.subscribe((user)=> {
    checkEmail(user.email).then((valid) => 
    {
        if(valid) {
            this.email = user.email
            this.url = 'http://localhost/check.php' + this.email; 
        }
    }
})
}

checkEmail(email: string): Promise<boolean>
{
    let valid = false;
    //email valid? -> verify the email is valid, if yes return true

    return valid;

}

-> not tested

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