当我登录时,我无法在不刷新页面的情况下将授权信息传递给我的应用程序。
登录服务;
loginService(auth: Auth): Observable<ResponseModel> {
return this.apollo
.mutate<any>({
mutation: this.LOGIN_USER,
variables: {
auth,
},
})
.pipe(
map((result) => {
return result.data.login;
})
);
}
登录组件;
login() {
this.isLoading = true;
if (this.form.invalid) {
this._snackBar.open('Lütfen tüm alanları doldurun.', 'Tamam', {
duration: 3000,
});
this.isLoading = false;
return;
}
const auth = {
email: this.form.value.email,
password: this.form.value.password,
};
this.authService.loginService(auth).subscribe((data) => {
if (!data.isSuccessful) {
this._snackBar.open(data.message, 'Tamam', {
duration: 2000,
});
this.isLoading = false;
return;
}
this._snackBar.open(data.message, 'Tamam', {
duration: 2000,
});
localStorage.setItem('accessToken', data.data);
this.router.navigate(['/']);
this.isLoading = false;
});
}
就像我说的,当我刷新页面时一切都解决了。
正常情况下,我登录站点时,需要直接从本地存储中获取授权信息,并将授权信息上报给api。
我解决了这个问题,实际上我遇到这个问题是因为我忽略了一些非常简单的事情。登录时,我只在重定向的按钮上使用了点击事件。当我在其中放一个标签并制作
href='/'
.时,问题就解决了