【问题标题】:Can't access public property in authService from one of its methods无法通过其中一种方法访问 authService 中的公共属性
【发布时间】:2019-04-07 08:47:05
【问题描述】:

我正在创建一个角度身份验证服务。 我有一个带有公共变量的服务(我需要公共变量,因为我在许多组件中使用它)是登录用户,我想在登录/注销后更新这个变量。

@Injectable()
export class AuthService implements OnInit{
  public loggedUser: firebase.User;

  constructor(private firebaseAuth: AngularFireAuth) {
    this.loggedUser = firebase.auth().currentUser;
  } ....

我拥有服务中的所有功能:

signup(email: string, password: string) {
    this.firebaseAuth
      .auth
      .createUserWithEmailAndPassword(email, password)
      .then(value => {
        console.log('Success!', value);
        this.loggedUser = firebase.auth().currentUser;
      })
      .catch(err => {
        console.warn('Something went wrong:',err.message);
      });    
  }

  login(email: string, password: string) {
    this.firebaseAuth
      .auth
      .signInWithEmailAndPassword(email, password)
      .then(value => {
        console.log('Nice, it worked!');
        this.loggedUser = firebase.auth().currentUser;
      })
      .catch(err => {
        console.warn('Something went wrong:',err.message);
      });
  }

他们工作顺利。但是使用 facebook 身份验证:

facebooklogin(){
    var provider = new firebase.auth.FacebookAuthProvider();
    this.firebaseAuth
    .auth
    .signInWithPopup(provider).then(function(result) {
      // This gives you a Facebook Access Token. You can use it to access the Facebook API.
      var token = (<any> result).credential.accessToken;
      // The signed-in user info.
      var userResult = result.user;
      this.loggedUser = firebase.auth().currentUser;
      console.log(userResult,token)
    }).catch(function(error) {
      // Handle Errors here.
      var errorCode = error.code;
      var errorMessage = error.message;
      // The email of the user's account used.
      var email = error.email;
      // The firebase.auth.AuthCredential type that was used.
      var credential = error.credential;
      // ...
      console.warn(errorCode,errorMessage,email,credential)
    });
  }

将 currentUser 分配给 loggedUser 时出现错误:“无法设置未定义的属性 'loggedUser'”。所以就像它不被识别一样。如果我 console.log 这个,我实际上是不确定的。怎么样?

【问题讨论】:

    标签: angular typescript firebase firebase-authentication


    【解决方案1】:

    您应该为此使用箭头函数,就像您之前的 2 个函数一样

    facebooklogin(){
        var provider = new firebase.auth.FacebookAuthProvider();
        this.firebaseAuth
        .auth
        .signInWithPopup(provider).then(result => {
    
        }).catch(error =>{
    
        });
      }
    

    或者如果您希望以相同的方式进行,首先在回调外部定义一个变量 self,然后在回调中引用 self

    facebooklogin(){
        var self = this
        var provider = new firebase.auth.FacebookAuthProvider();
        this.firebaseAuth
        .auth
        .signInWithPopup(provider).then(function(result) {
               //self.loggedin
        }).catch(function(error) {
    
        });
      }
    

    【讨论】:

      猜你喜欢
      • 2018-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多