shamon shamsudeen
shamon shamsudeen

Reputation: 5858

angular5: Type 'true' is not assignable to type 'Observable<boolean>

I am trying to change the login status after the user login

component.ts

isLoggedIn$: Observable<boolean>; 

  constructor (private auth:Auth){}

  ngOnInit (){


     this.isLoggedIn$ = this.auth.getUser()

     console.log(this.isLoggedIn$);

  }

Auth.ts

@Injectable()
export class Auth {

    private user:boolean = false;

     setUser(){

        this.user= true;
     }

     getUser(){
        return this.user
     }

    removeUser(){
        this.user = false;
    }

}

But i got the following error

error TS2322: Type 'true' is not assignable to type 'Observable'

Upvotes: 1

Views: 4734

Answers (1)

sabithpocker
sabithpocker

Reputation: 15566

If you are mocking http without an actual service, you can wrap the loggedIn status in an Observable as shown below for a quick fix. Take a look at rxjs and understand Observables for a better understanding.

@Injectable()
export class Auth {

    private user:boolean = false;

     setUser(){

        this.user= true;
     }

     getUser(): Observable<boolean>{
        return Observable.of(this.user)
     }

    removeUser(){
        this.user = false;
    }

}

Also: console.log(this.isLoggedIn$) will not work like that.

Upvotes: 1

Related Questions