angular - 第二次之后,如果我使用订阅,用户可以登录
问题描述
当我使用这个模型时,我没有问题。
login(userName: string, password: string): void {
if (!userName || !password) {
this.messageService.addMessage('Please enter your userName and password');
return;
}
if (userName === 'admin') {
this.currentUser = {
id: 1,
userName: userName,
isAdmin: true
};
this.messageService.addMessage('Admin login');
return;
}
this.currentUser = {
id: 2,
userName: userName,
isAdmin: false
};
this.messageService.addMessage(`User: ${this.currentUser.userName} logged in`);
}
并且用户从第一次登录,但是我现在实现了 web api 并且正在返回数据。当我在订阅方法中使用相同的代码时,我第一次无法登录,即使用户名和密码在第二次登录尝试后也是正确的我可以登录。为什么我的错误在哪里?
constructor(private messageService: MessageService,
private userService: UserService) { }
login(userName: string, password: string): void {
this.userService.getAllUsers().subscribe(users => {
this.allUsers = users;
let findUserInfo = this.allUsers.filter(user => user.userName == userName && user.password == password);
alert(findUserInfo);
console.log("******")
console.log(findUserInfo);
console.log("******")
if (!userName || !password) {
this.messageService.addMessage('Please enter your userName and password');
return;
}
if (userName === 'admin') {
this.currentUser = {
id: 1,
userName: userName,
isAdmin: true
};
this.messageService.addMessage('Admin login');
return;
}
this.currentUser = {
id: 2,
userName: userName,
isAdmin: false
};
this.messageService.addMessage(`User: ${this.currentUser.userName} logged in`);
});
解决方案
推荐阅读
- mathematical-optimization - Python PuLP 中的混合整数线性规划不可行解
- reactjs - Reactjs MobX 观察者重新渲染目标
- source-insight - source insight中的上下文窗口和关系窗口
- e2e-testing - 赛普拉斯管道 console.log 和命令日志输出
- git - 如何在合并到 master 之前测试 Bitbucket 分支代码?
- javascript - 在JS中将字符串转换为数组
- docker - 如何在 Windows 中本地使用 Docker
- php - 不显示数据库中行的特定空列
- php - 为什么模块可用于 PHP 命令行,而不是 PHP-Apache?
- javascript - 如何更新第一个 td 值 onclick 拖动时的方式