VUE 缓存中的数据跟随修改值动作联动

现象:取出缓存里的对象,将该对象赋给一个新的对象,修改新对象属性中的值,再次取出缓存时,缓存中对象的属性值也会跟随联动变化。

代码:

let allsmartdevice1 = this.$store.state.userProfile.Devices;

原因:这是一个引用传递而不是值传递,this.$store.state.userProfile.Devices和allsmartdevice1指向的是同一个内存地址。改变allsmartdevice1中对象的值,那么也会修改this.$store.state.userProfile.Devices的值。

解决方法:先把allsmartdevice1转换成字符串,然后在转换成对象

let allsmartdevice = JSON.parse(JSON.stringify(allsmartdevice1));
原文地址:https://www.cnblogs.com/duguangming/p/15431834.html