手动构造
小程序官当暂时没有提供状态管理模式,可以使用globalData实现一个简单的状态管理
// app.js
App({
globalData: {
count: 1 // 定义全局变量
},
// 初始化函数。进入页面 onShow() 阶段调用,在页面 data 中使用全局变量
initData(_this, params) {
let obj = {}
params.forEach((key) => {
obj[key] = this.globalData[key]
})
_this.setData(obj)
},
// 更新全局变量的值,同时更新页面 data 中的值
addCount(_this) {
this.globalData.count += 1
// 执行完后更新
this.initData(_this, ['count'])
}
})
在需要使用到全局变量页面的 onShow() 阶段,调用 app.initData() 将全局变量绑定到该页面视图层中
// home.js
const app = getApp()
Page({
data: {},
onShow: function () {
app.initData(this, ['count'])
},
addCount() {
app.addCount(this)
}
})
Westore
1.下载并引入 create.js、diff.js