[英]How do I access a specific property from a local storage object? (Angular 2/angular-2-local-storage)
So I'm storing an object into local storage using angular-2-local-storage as such: 因此,我使用angular-2-local-storage将对象存储到本地存储中 ,如下所示:
this.localStorage.set('myObject', {'prop1': "string", 'prop2': "string"});
Then I'm trying to get a specific property of that object as such: 然后,我试图像这样获取该对象的特定属性:
this.localStorage.get('myObject').prop1;
But doing this gives me the error Property 'prop1' does not exist on type '{}'
但是这样做给我的错误是
Property 'prop1' does not exist on type '{}'
I've also tried storing the object in a local variable, and then trying to access the property as such, but it gives me the same error. 我也尝试过将对象存储在局部变量中,然后尝试按原样访问属性,但这给了我同样的错误。
var myObject = this.localStorage.get('myObject'); var myProperty = myObject.prop1;
What am I doing wrong, and how do I access the data? 我在做什么错,如何访问数据?
I'm using localStorage.setItem / localStorage.getItem with Angular2, so it's maybe not the same behavior. 我在Angular2中使用localStorage.setItem / localStorage.getItem,所以可能不是相同的行为。 But have you tried by stringifying and parsing your object ?
但是,您是否尝试过对对象进行字符串化和解析?
In my case : 就我而言:
localStorage.setItem('myObject', JSON.stringify({'prop1': "string", 'prop2': "string"}));
let temp = JSON.parse(localStorage.getItem('myObject'));
temp.prop1; // gives "string"
In your case, something like : 就您而言,类似:
localStorage.set('myObject', JSON.stringify({'prop1': "string", 'prop2': "string"}));
let temp = JSON.parse(localStorage.get('myObject'));
temp.prop1;
Hope it cans help ! 希望可以帮到您!
You must save the object as a string 您必须将对象另存为字符串
localStorage.setItem('myObjectName', JSON.stringify(this.myObject));
and then obtain the string and convert it to an object 然后获取字符串并将其转换为对象
this.myObject = JSON.parse(localStorage.getItem('myObjectName'));
Can you provide more details about how you are configuring you angular2 local storage. 您能否提供有关如何配置angular2本地存储的更多详细信息。 I checked the source code and its doing the parsing and stringifing before you get or set respectively.I have not tested it but you should be able to do the following:
我分别在获取或设置之前检查了源代码及其解析和字符串化。我尚未对其进行测试,但是您应该能够执行以下操作:
import { LocalStorageModule } from 'angular-2-local-storage';
@NgModule({
imports: [
LocalStorageModule.withConfig({
prefix: 'my-app',
storageType: 'localStorage'
})
],
declarations: [
..
],
providers: [
..
],
bootstrap: [AppComponent]
})
export class AppModule {}
and then in you component you can inject this service as following 然后在您的组件中,您可以按以下方式注入此服务
constructor (
private localStorageService: LocalStorageService
) {
}
ngOnInit() {
this.localStorageService.set(key, value);
console.log(this.localStorageService.get(key));
}
You dont have to parse or stringify anything. 您不必解析或字符串化任何内容。 Hope this will help.
希望这会有所帮助。 AGAIN: I have not tested it this code is entirely based on angular-2-local-storage documentation.
再次:我尚未测试此代码完全基于angular-2-local-storage文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.