2017-10-15 339 views
0

我试图实现与角2.这里的Facebook登录是按钮登录Facebook的登录方法分配的对象:无法使用

<button ion-button (click)="fbLogin()"><ion-icon name="logo-facebook"></ion-icon>Login using Facebook</button> 

这里是Click事件处理程序:

fbLogin() { 
this.userLogin = new UserLogin(); 
     FB.login((response : any) => { 
      if (response.authResponse) { 
       FB.api('/me', { fields: ['first_name', 'last_name', 'gender', 'email', 'location', 'name', 'birthday'] }, function (response) { 
        let user : UserLogin = new UserLogin(); 
        console.log(this.userLogin); 
        user.Name = response.name || null; 
        user.Email = response.email || null; 
        user.ContactNumber = response.contactnumber || null; 
        user.DateOfBirth = response.birthday || null; 
        user.Gender = response.gender || null; 
        user.Country = response.location || null; 
        var accessToken = FB.getAuthResponse().accessToken; 
        console.log(accessToken); 
        this.userLogin = user; 

       }); 
      } else { 
       console.log('User cancelled login or did not fully authorize.'); 
      } 
     }); 
     if(this.userLogin.Email != null){ 
      this.accountService.Login(this.userLogin).subscribe(
       user => { 
        console.log(user); 
       }, 
       err => { 
        console.log(err); 
       } 
      ) 
     } 
    } 

用户登录后,将数据传递给user变量,然后分配给public userLogin: UserLogin;。问题是user被分配到userLogin,但由于响应迟来了,所以无法在FB.login()范围之外使用它。 可以建议,我如何在userLogin对象中保存响应,在同一个对象上继续进行FB.login()以外的其他操作?

回答

0

因为反应迟来了。可以建议,我怎样才能将响应保存在userLogin对象中,在同一对象上的FB.login()之外进一步操作?

只需从获取通过用户的回调函数中调用该函数即可。

+0

感谢您的回复,回调为我工作。进一步想知道为什么我不能在FB.login()内使用'this'变量? – Vivek