我刚刚开始使用ionic2,在我的应用程序中,我要求用户存储apiKey和秘密。
在本地存储中存储时工作正常,但在尝试使用存储值时遇到麻烦。
// Storing user apikey config
saveAPISettings(apiConfig) {
let apiSettings = { apiKey: apiConfig.apiKey, apiKeySecret: apiConfig.apiKeySecret};
this.storage.set('api-settings', JSON.stringify(apiSettings));
}
// Stored value
"api-settings": "{"apiKey":"2b749127b41927b491274b924927b412","apiKeySecret":"QEWIRP"}"
// Getting stored apikey config
getSettings(key) {
let results = [];
let object;
this.storage.get(key).then((val) => {
object = JSON.parse(val);
results['apiKey'] = object.apiKey;
results['apiKeySecret'] = object.apiKeySecret;
});
return results;
}
// getSettings usage
let config = getSettings('api-settings');
// getting undefined
console.log(config.apiKey);
or
console.log(config['apiKey']);
我在这里做错了什么? 谢谢你提前。
答案 0 :(得分:2)
无需将对象apiSettings字符串化为仅保存为对象
Mat gray,smallImg(cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1);
resize( gray, smallImg, smallImg.size(), 0,0, INTER_LINEAR);
Storage.get将解析一个承诺,只需在getSettings中返回存储
this.storage.set('api-settings', apiSettings);
在您的设置页面
getSettings(key) {
return this.storage.get(key)
}