ionic2本地存储获取
ionic2 local storage get
我刚开始使用 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']);
我做错了什么?
无需将对象 apiSettings 字符串化,只需另存为对象
this.storage.set('api-settings', apiSettings);
Storage.get 将解决承诺只是 return getSettings
中的存储
getSettings(key) {
return this.storage.get(key)
}
在您的设置页面中
getSettings('api-settings').then(val=>{
console.log(val.apiKey);
console.log(val.apiKeySecret);
});
我刚开始使用 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']);
我做错了什么?
无需将对象 apiSettings 字符串化,只需另存为对象
this.storage.set('api-settings', apiSettings);
Storage.get 将解决承诺只是 return getSettings
中的存储getSettings(key) {
return this.storage.get(key)
}
在您的设置页面中
getSettings('api-settings').then(val=>{
console.log(val.apiKey);
console.log(val.apiKeySecret);
});