I'm currently working on a project and have encountered an issue with utilizing localStorage
. My goal is to save the count state when the add button is clicked, so that even if I refresh the page, the number will remain intact.
--- cart.service.ts---
key= "count"
private Count = new BehaviorSubject<number>(this.getlocal());
Count$ = this.Count.asObservable();
incrementCount(){
const newCont = this.Count.value+1;
this.Count.next(newCont);
this.setlocal(newCont);
}
getlocal(): number {
const storedValue = localStorage.getItem(this.key);
return storedValue !== null ? parseInt(storedValue, 10) : 0;
}
setlocal(count: number) {
localStorage.setItem(this.key, count.toString());
}
Additionally, I have created an app component with the incrementcount function, along with a navcomponent that receives the count via HTML. However, I am encountering error ng0100. Any assistance in resolving this would be greatly appreciated!