したがって、angular-2-local-storageを使用してオブジェクトをローカルストレージに保存しています。
this.localStorage.set('myObject', {'prop1': "string", 'prop2': "string"});
次に、そのオブジェクトの特定のプロパティを取得しようとしています。
this.localStorage.get('myObject').prop1;
しかし、これを行うとエラーが発生します Property 'prop1' does not exist on type '{}'
また、オブジェクトをローカル変数に格納してから、そのようにプロパティにアクセスしようとしましたが、同じエラーが発生します。
var myObject = this.localStorage.get('myObject'); var myProperty = myObject.prop1;
何が間違っているのですか、またデータにアクセスするにはどうすればよいですか?
Angular2でlocalStorage.setItem / localStorage.getItemを使用しているので、同じ動作ではない可能性があります。しかし、オブジェクトを文字列化して解析してみましたか?
私の場合 :
localStorage.setItem('myObject', JSON.stringify({'prop1': "string", 'prop2': "string"}));
let temp = JSON.parse(localStorage.getItem('myObject'));
temp.prop1; // gives "string"
あなたの場合、次のようなものです。
localStorage.set('myObject', JSON.stringify({'prop1': "string", 'prop2': "string"}));
let temp = JSON.parse(localStorage.get('myObject'));
temp.prop1;
それが役立つことを願っています!
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加