在我的accountService中,我有一个对话框,询问用户返回promise的用户名/密码。如果他们点击关闭而不是解雇并且验证者在他们可以点击关闭之前需要数据,那么我想使用返回可观察数据的数据登录。
loginModal() : boolean {
this.dpDialogService.input(
'Login',
[
{
label: 'Username/Email',
form: 'username',
data: '',
validators: [Validators.required],
validatorMsg: 'Username is required.'
},
{
label: 'Password',
form: 'password',
data: '',
validators: [Validators.required],
validatorMsg: 'Password is required.',
type: 'password'
},
]
).then(
close => {
this.login({username: close.username.value, password: close.password.value}).subscribe(
loginResult => {
return true;
},
errorResult => {
this.dpDialogService.error('Invalid username/password.');
return false;
}
)
},
dismiss => {
return false;
}
);
}
如何从函数返回此布尔值。我猜我必须使用一个承诺,但不确定它们是如何嵌套的。
答案 0 :(得分:3)
如果.subscribe
未返回承诺,那么您需要根据.subscribe
我假设返回的承诺需要始终解决,绝不拒绝(基于dismiss =>
代码
).then(
close => {
return new Promise((resolve, reject) => {
this.login({username: close.username.value, password: close.password.value}).subscribe(
loginResult => {
resolve(true);
},
errorResult => {
this.dpDialogService.error('Invalid username/password.');
resolve(false);
}
)
})
},
dismiss => {
return false;
}
);
答案 1 :(得分:0)
这是答案,感谢Jaromanda X的帮助
loginModal() : Promise<boolean> {
return new Promise((resolve, reject) => {
this.dpDialogService.input(
'Login',
[
{
label: 'Username/Email',
form: 'username',
data: '',
validators: [Validators.required],
validatorMsg: 'Username is required.'
},
{
label: 'Password',
form: 'password',
data: '',
validators: [Validators.required],
validatorMsg: 'Password is required.',
type: 'password'
},
]
).then(
close => {
this.login({username: close.username.value, password: close.password.value}).subscribe(
loginResult => {
resolve(true);
},
errorResult => {
this.dpDialogService.error('Invalid username/password.');
resolve(false);
}
)
},
dismiss => {
resolve(false);
}
);
});