4
我確信有一個簡單的方法可以做到這一點,但我似乎無法找到它。這裏是我的代碼Angular2:在匿名函數中訪問類變量
export class UserLoginComponent {
private user: User;
public authService: AuthService;
constructor(private cognitoConfigs: CognitoUtil, authService: AuthService) {
this.user = new User();
this.authService = authService;
}
authenticate() {
// Some work being done here
let cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function(result: any) {
this.authService.login(this.user, result);
},
onFailure: function(err: any) {
console.log(err.message);
},
});
}
}
問題:在回調的onSuccess我無法訪問屬於它的父類的this.authService
變量。