分享一段简单的响应式对象监听代码带缓存Storage,可供大家参考与反思
WatchService
import { Cache } from "@/utils/tools/cache";
import { reactive, watch } from "vue";
class WatchService {
constructor() {
// this.obj = this.caches.userInfo.isEmpty ? { id: undefined, name: undefined } : this.caches.userInfo.json;
}
/**
* 创建缓存对象Storage
*/
caches = {
userName: new Cache('userName'),
userInfo: new Cache('userInfo')
}
/**
* 响应式对象用于watch(可从缓存读取)
*/
obj = reactive({
userName: this.caches.userName.value,
userInfo: this.caches.userInfo.isEmpty ? { id: undefined, name: undefined } : this.caches.userInfo.json
})
/**
* 注册事件watch(可缓存监听对象)
*/
events = {
userName: (callback?) => this.setWatch('userName', callback),
userInfo: (callback?) => this.setWatch(&#