查了很多资料,关于mobx的autorun方法,在网上很少,自己写了这一篇
在mobx里面autoRun基本上等同于watch了
上面是一个mbxData的定义,就不写了
。。。。。。
let mbxData = new MobxData();
autorun(() => {
console.log("autorun")
if (mbxData.quan) {
mbxData.goods.forEach((item) => {
item.set("check", mbxData.quan);
})
}
})
export default mbxData;
这个autoRun 可以监听多个元素,有点像computed 的 get,但是不需要返回值,只需要检测变化,并且做出相应处理即可
mobx里面computed
@computed get all() {
var temp = 0;
this.goods.forEach((item) => {
if (item.get("check")) {
temp += item.get("count") * item.get("price");
}
})
return temp;
}
和VUE里面的computed是一样的,检测多个因素变化,并且需要返回值