如何解决this setData污染全局属性globalData?

小程序 文章 2022-04-01 10:40 0 全屏看文

如何解决this setData污染全局属性globalData?How to solve this setData pollution global property globalData?

如何解决this setData污染全局属性globalData?

1.页面加载时使用this.setData将globalData.list赋值给data.list

2.使用过程中修改data.list的值,如添加一个对象

3.修改后log会看到globalData.list的值也被同步修改了

问题:

请问如何只修改data.list的值,而不修改globalData.list,始终保持globalData.list的值和加载页面之前一样

1. Use this when loading the page SetData will GlobalData List is assigned to data list 2. Modify data during use List value, such as adding an object 3 After modification, the log will see GlobalData The value of list has also been modified synchronously. Question: how can I modify only data List without modifying GlobalData List, always keep GlobalData The value of list is the same as before loading the page

回答:

梦罢:

this.setData({

"data.list":JSON.parse(JSON.stringify(globalData.list))

})

智友:非常好用,这样就纯对象或数组了,避免了this指向
brave:

百度搜索了解下深拷贝吧

智友:谢谢您

-EOF-