私はキー値を設定したいが、それがまだ存在しない場合にのみ実行したい。キーがローカルストレージに既に存在するかどうかを調べるには?
私のcomponent1.ts
ファイルでは、コンストラクタでキーと値を設定していますが、キーが前に作成されていない場合にのみこのコマンドを実行する必要があることを確認します。キーがすでに存在する場合は、実行しないでください。以下は
は私のコードです:
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
myname='';
list: Array<string> = ['a','b','c'];
sectionObj = {};
getresume = {};
stored_name = '';
constructor(public navCtrl: NavController) {
this.sectionObj = { "name" : this.myname,
"contactno" : "0",
"email" : "[email protected]"
};
// below line should only execute if "resume" key does not exist
localStorage.setItem("resume",JSON.stringify(this.sectionObj));
}
}
localstorage.setItem
関数を呼び出す前localstorage.getItem
機能を使用してのlocalStorageで値の
の可能性のある重複した[HTML5のlocalStorage:項目が設定されているかどうかを確認](https://stackoverflow.com/questions/3262605/html5-localstorage-check-if-item-is-set)を使用している – anoop