如何在角度破坏局部存储项目



我想在路由到其他组件时删除或破坏我的localstorage内容。我想实现这一目标,以免局部存储变量存储以前的值,即使是新值也出现。

我在正确的道路上吗?

游戏组件

export class Game implements OnDestroy{    
    constructor(){   
        this.currentGame = JSON.parse(localStorage.getItem('currentGame'));  
    }
    ngOnDestroy(){     
        this.currentGame = null;   
    }
}

您可以使用

localStorage.removeItem('currentGame');

另外,您也可以用

清除整个局部距离
localStorage.clear();

最新更新