[英]Angular Update value in localStorage
The problem I'm having, I can't add/change a given item inside a JSON object. 我遇到的问题是,我无法在JSON对象内添加/更改给定的项目。 I tried to convert it to use a simple
put
but that doesn't work since: Property "put" does not exist in type of "JSON"
我尝试将其转换为使用简单的
put
但是由于以下原因而无法正常工作: Property "put" does not exist in type of "JSON"
Key: 123 Value:{"name":"123","email":"123","password":"123","country":"123","about":"123","image":""}
My method to change the country value. 我更改国家/地区值的方法。
newJson:JSON;
onSubmit(value:any){
this.newJson = JSON.parse(localStorage.getItem(this.id));
this.newJson.put("country", value.country);
localStorage.setItem(JSON.stringify(this.newJson));
}
The value I get from the parameter (the submitted value) 我从参数获得的值(提交的值)
{"country":"Portugal"}
Try like this : 尝试这样:
export class Component {
private jsonObj: any = {};
private key: string = "123";
constructor(){
this.jsonObj = {
"name": "123",
"email": "123",
"password": "123",
"country": "123",
"about": "123",
"image": ""
}
localStorage.setItem(this.key, JSON.stringify(this.jsonObj));
}
ngOnInit() {
let localStorageJsonData = JSON.parse(localStorage.getItem(this.key));
localStorageJsonData.country = "france";
localStorage.setItem("obj", JSON.stringify(localStorageJsonData));
console.log('localStorageJsonData', localStorageJsonData);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.