封装状态管理
简易版数据管理
/**
* 全局数据保存并绑定this.data中的数据
* @this {this} Page中的this,需要使用store.call绑定this
* @example
* store.call(this,{example:'2'})
* @param {String} data 需要全局保存的数据
*/
const store = function (data){
const _data = this.data
const globalData = app.globalData
for (let prop in data) {
globalData[prop] = data[prop]
_data[prop] = data[prop]
}
this?.setData(_data)
console.log('globalData ------', app.globalData)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17