我的导航栏上有一个0x48
,还有两个服务:
在login()
(navbar.component
)中我会调用上面提到的两个服务,但我需要在login()
函数中首先执行身份验证然后等到完成然后调用第二个服务(授权) )。
myLogin()
目前,login() {
this.auth.login(); // <--- void type
this.user.authorization(nickname: string) // <--- subscribe
.subscribe(
(result) => this.res = result,
(err) => console.error(err),
() => console.log('done')
)
}
不等待第一次服务完成,这是我的问题。知道如何执行此任务吗?
这里是auth.login():&lt; ---- Authentication
login()
这是user.auth():&lt; ---授权
export class AuthenticationService {
userProfile: any;
lock = new Auth0Lock('xxxx', 'yyyy', {});
constructor( ) {
this.lock.on("authenticated", (authResult: any) => {
this.lock.getProfile(authResult.idToken, function(error:any, profile:any){
if(error) {
throw new Error(error);
}
localStorage.setItem('id_token', authResult.idToken);
this.userProfile = JSON.stringify(profile);
localStorage.setItem('profile', this.userProfile);
});
});
});
}
login() {
this.lock.show();
})
答案 0 :(得分:0)
在导航栏组件中尝试此操作:
login() {
Observable.forkJoin(
this.auth.login();
this.user.authorization(nickname: string);
).subscribe((info) => {
// info returns an array with two row, first contain result of
// this.auth.login() and second contains this.user.authorization(nickname).
}
}
答案 1 :(得分:-1)
this.auth.login()
.switchMap(this.user.authorization())
.subscribe(
(result) => this.res = result,
(err) => console.error(err),
() => console.log('done')
)
您可以将锁定事件转变为承诺并从登录返回承诺。
function login() {
return new Promise((resolve, reject) => {
this.lock.on('authenticated', resolve);
this.lock.on('authorization_error', reject);
this.lock.show();
});
}
login()
.then(() => this.user.authorization()}
.catch(err => console.log('error'))