在刚开始学习全局数据共享时,就有一个疑问,今天一实践果然还是有问题:在store.js中定义了字段和方法,在页面中使用时不识别,字段是“not defined”,方法是“not a function”。
import { createStoreBindings } from 'mobx-miniprogram-bindings'
import { store } from '../../store/store'
Page({
data: {
},
onLoad: function (options) {
this.storeBindings = createStoreBindings(this, {
store,
fields: ['num1', 'num2', 'sum'],
actions: ['updateNum1']
})
},
btnHandler1(e) {
this.updateNum1(e.target.dataset.step)
},
onUnload: function () {
this.storeBindings.detroyStoreBindings()
}
})
就比如以上的代码,最常见的例子。在页面onLoad()中的 this.storeBindings中,引用了actions: ['updateNum1'],但是这个updateNum1并没有声明啊,在下面的 btnHandler1(e)怎么能直接引用呢?反正我的不出意外报错“updateNum1 is not a function”。不仅在onLoad()之外我的报错,在onLoad() 之内也报错,因为它在createStoreBindings(this, {}中的呀。
不仅如此,字段也是一样,fields: ['num1', 'num2', 'sum']这三个字段全部报错:“…is not defined”