1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

Unable to get values from Service in Angular

Discussão em 'Angular' iniciado por papun, Setembro 10, 2024.

  1. papun

    papun Guest

    I have created and application with Login Functionality and want to get the logged In user name in the components

    Service File:

    login({ username, password }: any): Observable<any> {
    if (username === 'admin@gmail.com' && password === 'admin123') {
    this.setToken('abcdefghijklmnopqrstuvwxyz');
    **this.username1 = username;**
    this.setUserName(username);
    return of({ name: 'Papun Sahoo', email: 'admin@gmail.com' });
    }
    return throwError(new Error('Failed to login'));


    }

    On the Component I am trying to access the value

    userName?:string;
    constructor(private observer: BreakpointObserver, private router: Router,private loginService: LoginService) {
    **this.userName = this.loginService.getUserName()!;**
    console.log("From Component :"+this.loginService.username1);
    }


    but i am getting Undefined here

    From Component :undefined

    StackBlitz Link to Reproduce

    Any help would be appreciated

    Continue reading...

Compartilhe esta Página