如何销毁localStorage项目 - Angular

Xam*_*vil 17 local-storage angular

这是我目前想要在我的组件中做的事情.每当我路由到不同的组件时,我想删除或销毁我的localStorage内容.我想实现这一目标,以避免localStorage变量存储以前的值,即使新值正在进入.我是否在正确的路径上?

游戏组件

export class Game implements OnDestroy{

 constructor(){

 this.currentGame = JSON.parse(localStorage.getItem('currentGame'));

}

ngOnDestroy(){

  this.currentGame = null;

}


}
Run Code Online (Sandbox Code Playgroud)

Deb*_*ppe 60

您可以使用

localStorage.removeItem('currentGame');
Run Code Online (Sandbox Code Playgroud)

或者,您也可以清除整个localStorage

localStorage.clear();
Run Code Online (Sandbox Code Playgroud)