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()
以外的其他操作?
感谢您的回复,回调为我工作。进一步想知道为什么我不能在FB.login()内使用'this'变量? – Vivek