How to destroy localStorage item- Angular

元气小坏坏 提交于 2020-12-29 09:00:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!