When I invoke the add method in my template, it is supposed to allow me to add new objects into the eintraege-array. According to sources online, this data should be stored using LocalStorage so that when I refresh the page, all my changes are saved. However, this doesn't seem to be happening. Below is the add method called in a component, which then triggers another saveData method from one of the LocalStorage-Methods.
eintraege: Eintrag[] = [];
add(beschreibung: string, faelligkeitsdatum: string) {
var eintrag: Eintrag = {
beschreibung: '',
faelligkeitsdatum: '',
erledigt: false,
};
eintrag.beschreibung = beschreibung;
eintrag.faelligkeitsdatum = faelligkeitsdatum;
eintrag.erledigt = false;
this.eintraege.unshift(eintrag);
this.eintragService.saveData('token', JSON.stringify(this.eintraege))
Here is an example of my service using LocalStorage:
public saveData(key: string, value: string) {
localStorage.setItem('token', value);
}
EDIT: I also use the getItem Method:
ngOnInit(): void {
this.getEintraege();
}
getEintraege(): void {
this.eintragService.getData('token')
}
In my service, the getData method is implemented as follows:
public getData(key: string) {
return localStorage.getItem(key)
}
Despite these steps, upon refreshing the page, all data seems to disappear.