[英]what happens if we does not subscribe to HttpClient request which return observables in angular
[英]Angular HttpClient get request no return in subscribe
我想實現登錄。 因此,我發出了獲取用戶信息並將它們寫入本地存儲的請求,但不知何故,它從未達到我想要調用 setItem 的地步。
身份驗證服務.ts
export class AuthenticationService {
constructor(private http: HttpClient) {}
login(username, password) {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
Authorization: 'Basic ' + btoa('' + username + ':' + password + ''),
}),
};
this.http.get('api/user/loginInfo', httpOptions).subscribe((res) => {
localStorage.setItem('currentUser', JSON.stringify(res));
});
}
}
登錄組件.ts
export class LoginComponent implements OnInit {
constructor(private authService: AuthenticationService) {}
ngOnInit(): void {}
login() {
const username = (document.getElementById('username') as HTMLInputElement)
.value;
const password = (document.getElementById('password') as HTMLInputElement)
.value;
this.authService.login(username, password);
}
}
我犯了一個愚蠢的錯誤。 我從表單中調用了方法,但不知何故存在問題。 我現在已經刪除了 html 中的表單標簽,現在一切正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.