I'm trying to implement in my LoginService a isLoggedIn
boolean value of type BehaviorSubject
together with getter and setter functions to get the value as an Observable / set the variable correctly via its BehaviorSubject. This is working, however it throws two errors in TSLint about "Type not assignable" and "Dublicate identifier". What would be the right way to define it without TSLint complaining about.
This is a stripped down version of the above mentioned code:
@Injectable()
export class LoginService {
public isLoggedInSource = new BehaviorSubject<boolean>(false);
public isLoggedIn: Observable<boolean> = this.isLoggedInSource.asObservable(); // Duplicate identifier 'isLoggedIn'.
constructor(private http: Http) {}
set isLoggedIn(logged): void { // Duplicate identifier 'isLoggedIn'.
this.isLoggedInSource.next(logged);
}
get isLoggedIn(): Observable<boolean> { // Duplicate identifier 'isLoggedIn'.
return this.isLoggedInSource.asObservable();
}
logout() {
this.isLoggedIn = false; // Type 'boolean' is not assignable to type 'Observable<boolean>'.
}
login(body) {
return this.http.post('/login', body)
.map(res => {
if (res.token) {
this.isLoggedIn = true; // Type 'boolean' is not assignable to type 'Observable<boolean>'.
}
return res;
})
.catch(err => Observable.throw(err););
}
}