「抠抠记账反思」 localstorage 保存对象的坑

271 阅读1分钟

注意:当存入 localstorage 里的数据是对象时,注意 第二次更改里面的值,会影响第一次。 因为对象的地址不会变!!!! 所以需要深拷贝!

 saveAccount() {
        this.accountList.push(this.account);


    }

深拷贝:

saveAccount() {
  // 深拷贝,重新parse成一个对象
      const newAccount = JSON.parse(JSON.stringify(this.account))
      this.accountList.push(newAccount);
      console.log(this.accountList)

    }