问题
This is what i currently want to do in my component. 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;
}
}
回答1:
you can use
localStorage.removeItem('currentGame');
Alternatively, you can also clear the whole localStorage with
localStorage.clear();
来源:https://stackoverflow.com/questions/43637300/how-to-destroy-localstorage-item-angular