I want to have a page-reload proof class instances in my Angular 2 application.
In my component\'s .ts file I have classes:
export class AComponent i
I would suggest with angular-2-local-storage node_module.
Steps:
npm install angular-2-local-storage
Import the module and service as below
import { LocalStorageModule,LocalStorageService} from 'angular-2-local-storage';
Add the module to imports array and service to providers array as
imports: [ BrowserModule,
LocalStorageModule.withConfig({storageType: 'localStorage'}), ],
providers:[LocalStorageService],
Inject the Service as a dependency into component as below
constructor(private localStorageService: LocalStorageService) {
this.name = 'Angular-2-Local-Storage-Demo';
this.localStorageService.add('a',this.user);
console.log(this.localStorageService.get('a'));
this.valuFromLocalStorage= this.localStorageService.get('a')
}
LIVE DEMO