本文介绍了如何销毁localStorage物品-Angular的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我当前要在组件中执行的操作.每当我路由到其他组件时,我都想删除或销毁localStorage内容.我想要实现这一点,以便避免localStorage变量存储以前的值,即使有新值出现.我在正确的路径上吗?

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?

游戏组件

export class Game implements OnDestroy{

 constructor(){

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

}

ngOnDestroy(){

  this.currentGame = null;

}


}

推荐答案

您可以使用

localStorage.removeItem('currentGame');

或者,您也可以使用以下方法清除整个localStorage

Alternatively, you can also clear the whole localStorage with

localStorage.clear();

这篇关于如何销毁localStorage物品-Angular的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 23:55