I have two component at same label.I want to pass data from one component to another component.I am able to set data to service.But get from service is not working for me.I want to set data from login.component.ts and get data from managerdashboard.component.ts.How can I fix the issue.I am able to set data to service but I am not getting data from service.Please Help me.
sso.service.ts
import { Injectable } from '@angular/core';
export interface User {
userId:number;
aemUserId:string;
role: string;
authorization: string;
userName: string;
}
@Injectable()
export class SsoService {
public user:User;
public checkedDatas:Array<any> = [];
setUser( user ) {
debugger;
this.user = user;
console.log('setUser : ',this.user);
}
getUser() {
console.log('getUser : ',this.user);
return this.user;
}
isLoggedIn() {
return this.user && this.user.userName;
}
}
app.router.ts
import {ModuleWithProviders} from '@angular/core';
import {Routes,RouterModule, CanActivate} from '@angular/router';
import {AppComponent} from './app.component';
import { LoginComponent } from './login/login.component';
import { ManagerdashboardComponent } from './managerdashboard/managerdashboard.component';
export const router:Routes = [
{ path:'', redirectTo:'login', pathMatch:'full' },
{ path: 'login', component: LoginComponent },
{
path: 'aem',
component: AemHomeComponent,
children: [
{ path:'manager',component:ManagerdashboardComponent },
] },
// { path: '**', component: NotFoundComponent}
];
export const routes:ModuleWithProviders = RouterModule.forRoot(router);
login.component.ts
getSSO( ssoid ){
console.log('getSSO new : ',ssoid);
this.authGuard.getAuth(ssoid)
.subscribe( resp => {
console.log("getSSO response",resp);
here I am setting data to service
this.authService.setUser(resp);
console.log("14-08-2017",this.authService.getUser() )
this.router.navigate(
['aem', resp.role.toLowerCase()],
{ queryParams: { role:resp.role,sso_id:ssoid,auditorName:resp.userName}}
);
})
}
managerdashboard.component.ts
ngAfterViewInit(){
//Here I am getting data from service.
console.log("THIS IS MOHIT TRIPATHI",this.ssoservice.getUser());
}