This repository has been archived by the owner on Aug 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 108
期待数据监听更新啊 #42
Comments
我自己用的时候就觉得它缺少Redux那种store.subscribe()这种方法。老哥这个问题你解决了吗,你是如何解决的,还是使用了其他的状态管理库?望回复,分外感谢。 |
其实这种callback实现起来不算太难,遍历调用callback组就行 |
建议用Promise来解决这个问题: // 模拟请求
function mockRequest(){
return new Promise(r => {
setTimeout(r, 1000)
})
}
// app.js
App({
onLaunch(){
this.globalData.userInfoPromise = mockRequest().then(res => {
return new Promise(r => {
store.setState({
userInfo: res
}, () => r(res))
})
})
},
globalData: {
userInfoPromise: null
}
})
// pages/index/index.js
Page({
onLoad(){
getApp().globalData.userInfoPromise.then((userInfo) => {
// TODO 这里写userInfo获取后的操作
})
}
}) |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
在App.js的onLaunch()中请求后端获取userInfo。然后要在首页index.js中监听到userInfo后,才能进行其它的业务,可是目前没这个功能啊,总不能用定时器去循环等待吧,太美中不足了,期待能早点更新数据监听
The text was updated successfully, but these errors were encountered: