如何使用IONIC在cordova插件中访问此 [英] how to access THIS in cordova plugin with IONIC
本文介绍了如何使用IONIC在cordova插件中访问此的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用cordova插件来实现apple-sign-in方法,并将凭据设置为firebase。
i am trying to implement the apple-sign-in method using a cordova plugin and set the credentials to firebase.
我真正拥有的是:
constructor (
public afAuth: AngularFireAuth,
public afs: AngularFirestore,
@Inject(FirebaseApp) firebase: any
){
this.firebase = firebase;
}
loginApple(): Promise<boolean> {
return new Promise((resolve, reject) => {
cordova.plugins.SignInWithApple.signin({
requestedScopes: [0, 1]
}, function(succ){
var provider = new firebase.auth.OAuthProvider('apple.com').credential(succ.identityToken);
this.afAuth.auth.signinWithCredential(provider).then(result => {
//--> it seems the problem is here, because variable THIS is not available in the cordova plugin without a ionic-native wrapper <--
}).catch( error => {
reject( error.message || error );
})
}, function(err){
reject("Apple login failed");
})
})
}
推荐答案
当您使用 function
关键字定义回调时,此的含义会改变。防止这种情况的最简单方法是使用 fat arrow 符号定义函数:
The meaning of this
changes when you define a callback with the function
keyword. The simplest way to prevent that is to use fat arrow notation to define the function:
return new Promise((resolve, reject) => {
cordova.plugins.SignInWithApple.signin({
requestedScopes: [0, 1]
}, (succ) => { // change is here
var provider = new firebase.auth.OAuthProvider('apple.com').credential(succ.identityToken);
this.afAuth.auth.signinWithCredential(provider).then(result => {
}).catch( error => {
reject( error.message || error );
})
},(err) => { // changed here too, for consistence
reject("Apple login failed");
})
})
也请参阅有关问题原因的此答案以及其他解决方案:如何在回调中访问正确的 this?
Also see this answer on the cause of the problem, and other solutions: How to access the correct `this` inside a callback?
这篇关于如何使用IONIC在cordova插件中访问此的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文