localStorage中的Angular Update值

时间:2017-10-05 12:25:44

标签: arrays json angular typescript

我遇到的问题,我无法在JSON对象中添加/更改给定项目。我尝试将其转换为使用简单的boxD,但这不起作用,因为:put

Property "put" does not exist in type of "JSON"

我改变国家/地区价值的方法。

Key: 123 Value:{"name":"123","email":"123","password":"123","country":"123","about":"123","image":""}    

我从参数(提交的值)获得的值

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));
    }

1 个答案:

答案 0 :(得分:0)

试试这样:

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);
    }
}