【问题标题】:Store Auth0 user info into Angular2+ service将 Auth0 用户信息存储到 Angular2+ 服务中
【发布时间】:2018-11-15 23:58:04
【问题描述】:

我目前正在使用 Auth0 身份验证迁移现有的 Angular2+ 代码。 我必须放弃 Lock 小部件才能使用通用登录。

这是我使用 Auth0.js 的新服务示例:

  public infosUtilisateur: any = undefined;

  public RafraichirInfosUtilisateur()
  {
    let accessToken = localStorage.getItem('access_token');

    if(accessToken && !this.infosUtilisateur)
    {
      this.webAuth.client.userInfo(accessToken, function(err, user) 
      {
        this.infosUtilisateur = user; // Here goes the pain
      });
    }
  }

每当调用我的函数时,我都会收到此错误:

Cannot set property 'infosUtilisateur' of undefined

我不明白为什么我无法在 API 调用中访问我的服务属性。

我做错了什么?

【问题讨论】:

    标签: angular auth0


    【解决方案1】:

    使用不带function 关键字的callback function 并使用=> insead。这会保留调用上下文。

    if(accessToken && !this.infosUtilisateur)
    {
      this.webAuth.client.userInfo(accessToken, (err, user) =>  
      {
        this.infosUtilisateur = user; // Here goes the pain
      });
    }
    

    【讨论】:

    • 我不知道这两种语法有不同的行为,就像一个魅力谢谢你! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-27
    • 2017-01-19
    • 2020-10-08
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多