It's nothing related to the local storage. Your root component needs a trigger or refresh to access the local storage or any change in the data.
When you redirect to the home page only the code from the home page component is executed and not from the parent or root component.
You need to declare a variable in the service file. So whenever you update the local storage you will have to update the value of the service file variable. You can access this variable in your root component. So if there is any change in the service variable a trigger will be fired and you can update the local storage. Check the link for a sample code https://github.com/resistcheat/angular-variable-as-observable
Create a Service File
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { BehaviorSubject, Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class CommonService {
data: any;
private items: BehaviorSubject<any> = new BehaviorSubject<any>(this.data);
items$: Observable<any> = this.items.asObservable();
constructor(private http: HttpClient) { }
setLocalStorageData() {// Call this function to set localstorage
localStorage.setItem("oauth_token", oauth_token)
this.data = oauth_token;
}
}
//Trigger will be fired when this.data changes. Add the below code to your root Component
constructor(private commonService: CommonService) { }
items: any;
subscription: Subscription; //import { Subscription } from 'rxjs';
ngOnInit() {
this.subscription = this.commonService.items$.subscribe(items => {
this.items = JSON.stringify(items));
console.log(localStorage.getItem("oauth_token"));
}
}
ngOnDestroy() {
this.subscription && this.subscription.unsubscribe();
}