因为上一篇中代码相似度挺高,所以有了对ref与Storage的整合对象 CachedRef
上一篇:基于vue3的Storage对象监听服务 - WatchService_凤凰涅檠的博客-CSDN博客
CachedRef<T> 封装类
import { Ref, ref, UnwrapRef, watch } from "vue";
import { Cache } from "./cache";
/**
* Storage缓存ref,实现监听
*/
export class CachedRef<T extends Object> {
constructor(cachedKey, obj?, isCached?: boolean) {
isCached = isCached ?? true
if (isCached) {
this.cachedKey = cachedKey;
this.cachedObj = new Cache(cachedKey);
}
this.ref = ref<T>(this.cachedObj.isEmpty ? obj : this.cachedObj.json);
}
/**
* 缓存key
*/
cachedKey: string;
/**
* 缓存对象
*/
cachedObj: Cache;
/**
* ref对象
*/
ref: Ref&l