AuthService가 있고 LoginComponent에서 signInAction 메서드를 호출하고 있습니다. 토큰이 초기화 될 때 리디렉션하고 싶습니다.서비스가 Angular 4 애플리케이션에서 작업을 완료하면 리디렉션하려고합니다.
어떻게하면됩니까? 여기
내 auth.service.ts 여기signInAction(){
let that = this;
new Oidc.UserManager(this.config).signinRedirectCallback().then(function (user) {
that.currentUser = user;
that.token = user.access_token;
}).catch(function (e) {
console.error(e);
});
}
내 LoginComponent 파일
ngOnInit() {
this.authService.signInAction();
//Wait until signInAction is complete before navigateByUrl
this.router.navigateByUrl('/my-profile');
}
/업데이트] WORKING 버전 모두에/ 감사 인 파일에서 로그인의 방법, 내가 배운 Observable이 어떻게 작동하는지 그리고 이것이 나의 최종 작업 버전입니다. 내 LoginComponent에서 AuthService
signInAction(){
let that = this;
return Observable.create(observer =>
new Oidc.UserManager(this.config).signinRedirectCallback().then(function (user) {
that.currentUser = user;
that.token = user.access_token;
observer.next();
observer.complete();
}).catch(function (e) {
console.error(e);
observer.complete();
})
);
}
//에서
//
ngOnInit() {
this.authService.signInAction()
.subscribe(
() => this.router.navigateByUrl('/my-profile')
);
}
현재 작성한대로 할 수 없습니다. 프로세스가 완료되면 외부에서 알 수있는 방법이 없습니다. 'signInAction'에서 관찰 할 수있는 것을 반환 할 수 있습니다. 'this.authService.signInAction(). subscribe (() => this.router.navigateByUrl ('/ my-profile '))'). 또는 관찰 완료 가능한 이벤트를 통해 프로세스 완료를 노출합니다. 'this.authService.signInAction(); this.authService.signedIn $ .subscribe (...); ' – jonrsharpe