I am using getter and setter to set a field from the web cache, but I get the TypeError
when I try to use the setter for example. The error is:
ERROR TypeError: this.saveCache is not a function
The getter and setter are below:
get saveCache (): any {
if (localStorage.getItem('saveCache') === null) {
return null;
}
return JSON.parse(localStorage.getItem('saveCache'));
}
set saveCache(value:any) {
localStorage.setItem('saveCache', JSON.stringify(value));
}
When I try to set the saveCache
field, I get the above error. How can I solve this issue?
I added an example here with the error:
https://stackblitz.com/edit/angular-ivy-cryfvb?file=src%2Fapp%2Fapp.component.ts
A setter has to be used as below:
this.saveCache = this.fruits;
and not this.saveCache(this.fruits);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.