I want to delete or destroy my localStorage content whenever I route to a different component. I want to achieve this so as to avoid the localStorage variable from storing previous values even tho new values are coming in.
Am I on the right path?
Game Component
export class Game implements OnDestroy{
constructor(){
this.currentGame = JSON.parse(localStorage.getItem('currentGame'));
}
ngOnDestroy(){
this.currentGame = null;
}
}
forEach(k => localStorage. removeItem(k)) //Or use localStorage. clear() if you want to just clear everything.
removeItem("data") With localStorage. removeItem('data') we can remove the localStorage item. Its use case is similar to setItem() – when we want to remove an item, we click a button to complete that functionality.
you can use
localStorage.removeItem('currentGame');
Alternatively, you can also clear the whole localStorage with
localStorage.clear();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With