Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to destroy localStorage item- Angular

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;   
    }
}
like image 289
XamarinDevil Avatar asked Apr 26 '17 14:04

XamarinDevil


People also ask

How do I delete multiple items in localStorage?

forEach(k => localStorage. removeItem(k)) //Or use localStorage. clear() if you want to just clear everything.

How do I remove items from localStorage in react?

removeItem("data") With localStorage. removeItem('data') we can remove the localStorage item. Its use case is similar to setItem() – when we want to remove an item, we click a button to complete that functionality.


1 Answers

you can use

localStorage.removeItem('currentGame');

Alternatively, you can also clear the whole localStorage with

localStorage.clear();
like image 110
Deblaton Jean-Philippe Avatar answered Sep 22 '22 02:09

Deblaton Jean-Philippe