我正在尝试使用TypeScript装饰器在Angular应用程序中的localstorage中存储数据。这就是我想要实现的目标
export function Cache() {
return function (target, key): any {
// property value
let _val = target[key];
// property getter
const getter = function () {
const data = localStorage.get(key);
if (data) {
return data;
}
localStorage.set(key, _val);
return _val;
};
// property setter
const setter = function (newVal) {
localStorage.set(key, newVal);
console.log('Setting',newVal);
_val = newVal;
};
// Create new property with getter and setter
Object.defineProperty(target, key, {
configurable: true,
enumerable: true,
get: getter,
set: setter
});
};
}
并使用它: 第1部分:
....
@Cache() userSession: any = {val:''};
ngOnInit() {
this.userSession.val = "some val"
}
....
组件2:
....
@Cache() userSession: any;
ngOnInit() {
console.log('Stored value is',this.userSession);
// expected : {val:'some val'}
// prints : {val:''}
}
....
所以根据我的理解,只有当对象本身有任何赋值而不是它的属性时,才会调用属性setter。因此,这是有效的,
Object.assign(this.userSession , {val: 'some val'});
or
this.userSession = {val: 'some val'}
console.log(this.userSession) // prints: {val: 'some val'}
但这有点超过了我想在这里实现的目的。所以问题是,如何修改装饰器功能才能做到这一点?
this.userSession.val = 'some val';
任何指示赞赏。
答案 0 :(得分:0)
我没有以这种方式使用装饰器,但我很确定你需要删除属性才能替换它,所以改变它:
Object.defineProperty(target, key, {
get: getter,
set: setter,
enumerable: true,
configurable: true
});
对此(用删除包装):
if (delete this[key]) {
Object.defineProperty(target, key, {
get: getter,
set: setter,
enumerable: true,
configurable: true
});
}