首页 > 解决方案 > 第二次之后,如果我使用订阅,用户可以登录

问题描述

当我使用这个模型时,我没有问题。

  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`);
    });


标签: angular

解决方案


推荐阅读