Javascript 在Ionic构造函数中调用变量返回未定义的
如何在构造函数中调用变量Javascript 在Ionic构造函数中调用变量返回未定义的,javascript,cordova,variables,ionic-framework,constructor,Javascript,Cordova,Variables,Ionic Framework,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 W
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?
..........
..........
}
由于不可避免的原因,我无法添加
http://localhost/check.php“+此电子邮件
在{this.email=user.email;
之后,您可以尝试在页面事件的不同阶段触发哪个。可能是您可以尝试在页面事件的不同阶段触发哪个。您也可以在订阅者中进行调用。或者在您不知道设置了变量的情况下从内部调用函数
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
})
}
//编辑
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;
}
构造函数(私有身份验证:AngularFireAuth){
this.auth.authState.subscribe((用户)=>{
检查电子邮件(user.email)。然后((有效)=>
{
如果(有效){
this.email=user.email
this.url=http://localhost/check.php“+此电子邮件;
}
}
})
}
checkEmail(email:string):承诺
{
设有效=假;
//电子邮件是否有效?->验证电子邮件是否有效,如果是,返回true
返回有效;
}
->未测试您也可以在订阅服务器中进行调用。或者在您知道变量已设置的情况下从内部调用函数
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
})
}
//编辑
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;
}
构造函数(私有身份验证:AngularFireAuth){
this.auth.authState.subscribe((用户)=>{
检查电子邮件(user.email)。然后((有效)=>
{
如果(有效){
this.email=user.email
this.url=http://localhost/check.php“+此电子邮件;
}
}
})
}
checkEmail(email:string):承诺
{
设有效=假;
//电子邮件是否有效?->验证电子邮件是否有效,如果是,返回true
返回有效;
}
->未测试不可能…可观察项是异步的..您的
this.url
分配发生在调用订阅函数之前不可能…可观察项是异步的..您的this.url
分配发生在调用订阅函数之前如果我调用另一个externa,安全漏洞将打开//现在调用this.email
中的l域如果我在//现在调用this.email
中调用另一个外部域,将打开一个安全漏洞