我正在使用Ionic和Typescript。我需要访问following api:
load(key, successCallback/*(value)*/, failCallback)
在打字稿中,我执行以下操作:
cordova.plugins.icloudkv.load('key').then((data) => {
console.log(data);
alert('load key: ' + JSON.stringify(data));
});
}
}).catch((e) => {
console.error(JSON.stringify(e));
this.doAlert('iCloud: ' + JSON.stringify(e));
});
但是,alert
永远不会被解雇。
问题
有人可以建议使用打字稿来调用javascript函数的最佳方法是什么?
由于
答案 0 :(得分:2)
根据api,您提供的代码应该更像:
cordova.plugins.icloudkv.load('key',
(data)=>{
console.log(data);
alert('load key: ' + JSON.stringify(data));
}, (e)=>{
console.error(JSON.stringify(e));
this.doAlert('iCloud: ' + JSON.stringify(e));
});
答案 1 :(得分:1)
您是否在控制台中收到任何错误?
您的代码假定load
返回Promise。看看这是否有效,因为该示例明确要求load
方法中的两个回调:
cordova.plugins.icloudkv.load('key', (data) => {
console.log(data);
alert('load key: ' + JSON.stringify(data));
}, (e) => {
console.error(JSON.stringify(e));
this.doAlert('iCloud: ' + JSON.stringify(e));
});
答案 2 :(得分:0)
如果您更喜欢使用Promise
,即使api没有使用,也可以围绕通话构建Promise
:
new Promise((resolve, reject) => cordova.plugins.icloudkv.load('key', resolve, reject))
.then((data) => {
console.log(data);
alert('load key: ' + JSON.stringify(data));
});
}
}).catch((e) => {
console.error(JSON.stringify(e));
this.doAlert('iCloud: ' + JSON.stringify(e));
});