I have the following structure:
App
|
-> LoginComponent
|
-> HomeComponent
In my login I make a call to my userService in order to authenticate a user
as follows:
userService.login(this.form.value).subscribe(
data => {
console.log('Going to worry free with ', data);
router.navigate(['Home']);
},
err => {
console.log(err);
}
);
}
And in the login function I have something like:
return this.http.post(
this.baseApi + 'login',JSON.stringify({user:info.user, pass:info.pass}),
{
"headers" : headers
}
).map(data => {
this.userData = data.json();
return this.userData;
});
As you can see I have a variable in the UserService class called userData
in which I store the information that is returned back from my api endpoint
This line above:
console.log('Going to worry free with ', data);
Works just fine and returns me back the data the issue is, after i navigate
to home:
router.navigate(['Home']);
The data seems to be lost, as far as I know all services are singletons and
this should be the case.
In my home constructor I have something like this:
constructor(private userService: UserService, private router : Router){
console.log('Arrived at Home with ', userService.getUser())
if(typeof userService.getUser() === 'undefined') {
this.router.navigate(['Login']);
}
}
And the user is undefined. I have also placed the UserService as a provider
in the boot.js of my application as follows:
bootstrap(<any>AppComponent, [ROUTER_PROVIDERS, HTTP_PROVIDERS, UserService]);
Can someone please enlighten me in what I'm actually doing wrong?
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.