angular - 如何从订阅中设置全局变量的值
问题描述
我需要将 HTML 表单中提供的电子邮件地址与我从模拟 API 获得的电子邮件地址进行比较。如果电子邮件地址匹配,那么现在就足以成功授权。我试图通过isAuthorized
在loginUser()
方法中创建一个布尔变量来存档它,我将其初始值设置为false
,如果电子邮件地址匹配,我试图将其设置为true
在 http 请求的订阅中,但这不起作用.
如何实施上述授权?我是 Angular 的新手。
登录组件.ts
import { Component, OnInit, HostListener } from '@angular/core';
import { Router } from '@angular/router';
import { LoginStateService } from 'src/app/login-state.service'
import { UserService } from '../user.service';
import { HttpService } from '../http.service';
interface Users {
[key: string]: {
email?: string;
}
}
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css'],
})
export class LoginComponent implements OnInit {
loginStateText: string;
loginRout: string;
constructor(
private router: Router,
private user: UserService,
private loginState: LoginStateService,
private cssHelper: CssHelperService,
private http: HttpService) {
}
ngOnInit() {
this.loginState.currentButtonText.subscribe(
loginStateText => this.loginStateText = loginStateText
);
this.loginState.currentRout.subscribe(
loginRout => this.loginRout = loginRout
);
}
loginUser(e){
e.preventDefault;
let email: string = e.target.elements[0].value;
let isAuthorized: boolean = false;
this.http.getUserId().subscribe(data => {
alert('Input: ' + email + ' / JSON: ' + data[0].email);
if(email == data[0].email){
console.log('Auth OK');
isAuthorized = true;
}
else{
console.log('Auth not OK');
}
})
if(isAuthorized){
this.user.setUserLoggedIn();
this.loginState.ChangeLoginButtonText("Logout");
this.loginState.ChangeLoginRout('/logout');
alert("Authorization success");
this.router.navigate(["/dashboard"]);
}else{
alert('Authorization failed, please check your credentials.')
}
}
}
解决方案
您的逻辑应该在从服务器获取数据后执行。现在编写的代码不会等待 api 请求完成。
动起来
if(isAuthorized){
this.user.setUserLoggedIn();
this.loginState.ChangeLoginButtonText("Logout");
this.loginState.ChangeLoginRout('/logout');
alert("Authorization success");
this.router.navigate(["/dashboard"]);
}else{
alert('Authorization failed, please check your credentials.')
}
进入订阅回调。
loginUser(e){
e.preventDefault;
let email: string = e.target.elements[0].value;
let isAuthorized: boolean = false;
this.http.getUserId().subscribe(data => {
alert('Input: ' + email + ' / JSON: ' + data[0].email);
if(email == data[0].email){
console.log('Auth OK');
isAuthorized = true;
}
else{
console.log('Auth not OK');
}
if(isAuthorized){ // here your logic should execute
this.user.setUserLoggedIn();
this.loginState.ChangeLoginButtonText("Logout");
this.loginState.ChangeLoginRout('/logout');
alert("Authorization success");
this.router.navigate(["/dashboard"]);
}else{
alert('Authorization failed, please check your credentials.')
}
})
}
推荐阅读
- python - 如何将日期时间转换为日期?
- swift - 根据数组中的项目数迭代创建 UITextView
- javascript - 在浏览器中本地通过 DirectShow 播放 Videograbber 输入
- ajax - 使用邮递员 404 端点发现 api 身份验证
- javascript - 尝试从我的 S3 存储桶访问文件时在我的 Node.js 应用程序中获取“禁止:null”
- c# - 如果要删除某些元素,如何迭代 JObject?
- r - 我有 2 个数据框,每个数据框都有纬度和经度。我想为 df1 中的每个点找到离 df2 最近的 5 个点
- android - Firebase unity android 应用程序身份验证不起作用
- java - 从 JOIN 表中检索数据到 JSP
- ios - SwiftUI 表格:与 navigationBarItems 不一致的表格行为