世界上最小的小程序框架 - 29 行代码搞定全局状态管理和跨页通讯
export default {
motto: 'Hello World',
userInfo: {},
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo'),
logs: [],
logMotto:function(){
console.log(this.motto)
}
}
import store from '../../store'
import create from '../../utils/create'
const app = getApp()
create(Page, store, {
bindViewTap: function () {
wx.navigateTo({
url: '../logs/logs'
})
},
onLoad: function () {
if (app.globalData.userInfo) {
this.store.userInfo = app.globalData.userInfo
this.store.hasUserInfo = true
this.store.update()
} else if (this.data.canIUse) {
app.userInfoReadyCallback = res => {
this.store.userInfo = res.userInfo
this.store.hasUserInfo = true
this.store.update()
}
} else {
wx.getUserInfo({
success: res => {
app.globalData.userInfo = res.userInfo
this.store.userInfo = res.userInfo
this.store.hasUserInfo = true
this.store.update()
}
})
}
},
getUserInfo: function (e) {
app.globalData.userInfo = e.detail.userInfo
this.store.userInfo = e.detail.userInfo
this.store.hasUserInfo = true
this.store.update()
}
})
this.store.any_prop_you_want_to_change = 'any_thing_you_want_change_to'
this.store.update()
import create from '../../utils/create'
// components/hello/hello.js
create(Component, {
})
this.store.any_prop_you_want_to_change = 'any_thing_you_want_change_to'
this.store.update()
onShow:function(){
this.store.update()
}
在 onShow 的时候更新一下 store 就可以了。
MIT @dntzhang