[英]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 將解決一個承諾,只需返回 getSettings 中的存儲
getSettings(key) {
return this.storage.get(key)
}
在您的設置頁面
getSettings('api-settings').then(val=>{
console.log(val.apiKey);
console.log(val.apiKeySecret);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.