【发布时间】:2025-12-07 17:30:02
【问题描述】:
我在同一个标签上有两个组件。我想将数据从一个组件传递到另一个组件。我能够将数据设置为服务。但是从服务获取对我不起作用。我想从登录设置数据。 component.ts 并从 managerdashboard.component.ts 获取数据。如何解决此问题。我可以将数据设置为服务,但我没有从服务获取数据。请帮助我。
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());
}
【问题讨论】:
-
您是否收到任何控制台错误?为这个文件添加你的构造函数代码:
managerdashboard.component.ts
标签: angular typescript service