Angular 从服务获取组件中的变量值?
我试图在我的用户面板组件中获取返回用户的id,但是无论我尝试什么,它都返回未定义的Angular 从服务获取组件中的变量值?,angular,Angular,我试图在我的用户面板组件中获取返回用户的id,但是无论我尝试什么,它都返回未定义的 服务: public resData: any; public username: string; constructor(private http: HttpClient) { } authenticationService(username: string, password: string) { let basicAuthToken = this.createBasicAuthTo
服务:
public resData: any;
public username: string;
constructor(private http: HttpClient) { }
authenticationService(username: string, password: string) {
let basicAuthToken = this.createBasicAuthToken(username, password)
return this.http.post(`http://localhost:8080/api/users/login`, {username, password},
{ headers: { authorization: basicAuthToken } }).pipe(map((res: any) => {
this.resData = res;
this.username = res.username;
this.registerSuccessfulLogin(basicAuthToken);
}));
}
在我的组件中,我只是想把它打印出来
handleUpdate(){
console.log(this.authenticationService.id);
}
小伙子们,姑娘们,有什么建议吗?在这方面,您似乎没有订阅正在拨打的服务电话
始终需要订阅对后端进行的http api调用
只需将subscribe添加到函数call-in组件中,它就会工作您可能无法正确调用服务方法来获取id,通常是在其in构造函数(private authenticationService:authenticationService)中,然后是this.authenticationService.authenticationService.subscribe(data=>{data.id})之类的内容