私は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');
}
/UPDATED 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