【发布时间】:2018-10-12 04:24:29
【问题描述】:
我刚开始使用 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']);
我在这里做错了什么?
【问题讨论】:
标签: angular ionic2 local-storage