I have created an AuthService and I am looking to utilize it across all my components. My goal is to maintain a global userLoggedIn value for all components. However, I encountered the following error when running the script - Property 'userLoggedIn' does not exist on type 'AuthService'.
import { Component, Input, Inject, ReflectiveInjector, Injectable} from '@angular/core';
@Injectable()
export class AuthService {
static userLoggedIn : boolean = false;
static changeLoginStatus(status: boolean){
this.userLoggedIn = status;
}
}
Component file -
import { Component, OnInit, Inject, ReflectiveInjector } from '@angular/core';
import { FormGroup, FormBuilder, Validators, AbstractControl, FormControl } from '@angular/forms';
import { Http, Response, RequestOptions, Headers } from '@angular/http';
import {Observable} from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import { CustomValidators } from '../common/validations.ts';
import { AuthService } from '../injectables/authservice.ts';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
loginForm : FormGroup;
data : any;
constructor(private http: Http, fb: FormBuilder) {
this.loginForm = fb.group({
'email':[null, Validators.compose([Validators.required,CustomValidators.emailFormat])],
'password':[null, Validators.compose([Validators.required])],
});
}
ngOnInit() {
}
submitLoginForm(value: any){
console.log(value);
let headers = new Headers();
headers.append('Content-Type', 'application/json');
let body = JSON.stringify(value);
this.http.post(
'http://192.168.1.90/articles/data/post/',
body)
.subscribe((res: Response) => {
this.data = JSON.stringify(res);
console.log('---->'+res.json().data.email);
localStorage.setItem('email', res.json().data.email);
localStorage.setItem('userID', res.json().data.id);
localStorage.setItem('name', res.json().data.name);
localStorage.setItem('loginStatus', 'true');
AuthService.changeLoginStatus(true);
console.log('localstorege item ---->'+localStorage.getItem('email'));
});
return false;
}
}