繁体   English   中英

从服务中获取组件中的变量值?

[英]Get variable value in component from a service?

我试图在我的用户面板组件中获取返回用户的 id,但是无论我尝试什么,它都会返回undefined

服务:

  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 调用后端只需添加订阅 ZC1C425268E68385D1AB5074F1 组件,它应该可以工作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM