In an attempt to store a value in localStorage and retrieve it upon refresh, I have developed a local-storage service to set the value by calling the service.
When trying to retrieve the value on refresh, I found that my appComponent's ngOnInit method appeared as follows:
ngOnInit() {
// Service which returns undefined
console.log(this.ls.getLocalStorage('test'))
// local which returns value...
console.log(localStorage.getItem('test'));
}
Within my local-storage-service, the code is structured like so:
getLocalStorage(k) {
console.log(k);
localStorage.getItem(k);
}
Oddly enough, the first console log in my onInit function returns 'undefined' while the second one correctly displays the value. Why does this discrepancy exist and is there a potential resolution?
Any insights would be appreciated. Thank you.