As a beginner with Angular 2, I'm adapting well so far. However, I've encountered a problem (possibly due to my familiarity with Angular 1) - I need the class "User" to be global and accessible across different components.
Currently, I have a User
class and I'm logging in the user in one component.
@Component({
selector: 'login',
templateUrl: 'app/login/login.html',
providers: [User]
})
export class loginComponent {
submitted = false;
username : string;
password : string;
constructor(public user : User){}
onSubmit(event) { //form event
event.preventDefault();
this.user.login(this.username,this.password, () =>{this.router.navigateByUrl('dashboard')}
The login method in the user class is updating the internal variables of the user, such as name and last name. I have a different component assigned to the dashboard route in the router. However, when I import User
, a new instance of the class is created. How can I maintain the data in the class?