commonstore
v1.1.1
Published
study-mvvm@coderL
Downloads
5
Readme
实现
个人学习简单实现 watch监听数据 安装 npm i commonstore import CommonStore from 'commonstore'
1. 新建实例
const store = new CommonStore({
data: {
a: 1,
b: 2,
arr: [1, 2, 3],
},
});
2.实例继承 $watch 方法监听 data.a 值发生改变触发 callback回调
store.$watch("data.a", (old,val) => {
console.log(old);// 老的值
console.log(val);// 最新值
});
store.$watch("data.a", val => {
console.log(val);// 只有一个参数时 val为最新值
});
3.也可监听多个属性 监听 data.b
store.$watch("data.b", (val) => {
console.log(val);
});
2022.3.29 coderL