Upon logging into my app, I want to retrieve a JSON object stored in local storage and assign it to the user Object. The issue is that the first time I login, nothing is displayed. However, upon refreshing the page, I can see the first name of the user.
This is the .ts file:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-navbar',
templateUrl: './navbar.component.html',
styleUrls: ['./navbar.component.css']
})
export class NavbarComponent implements OnInit {
user: Object;
ngOnInit() {
this.user = JSON.parse(localStorage.getItem('user'));
}
}
When using it in the template, it should look like this:
<p> {{ user.first_name }} </p>
How can this issue be resolved?