How to make the user state persistent even the page reloads?
as @user184994 Mentioned NGRX
state is only held in memory. If you want it to persist between page refreshes Go for LocalStorage
or sessionStorage
localStorage
and sessionStorage
accomplish the exact same thing and have the same API, but with sessionStorage
the data is persisted only until the window or tab is closed, while with localStorage
the data is persisted until the user manually clears the browser cache or until your web app clears the data.
I would suggest you to go for @ngx-pwa/local-storage Async local storage for Angular
For Angular 5:
npm install @ngx-pwa/local-storage@5
Register it in your RootModule
import { LocalStorageModule } from '@ngx-pwa/local-storage';
@NgModule({
imports: [
BrowserModule,
LocalStorageModule,
...
]
Inject and use it
import { LocalStorage } from '@ngx-pwa/local-storage';
@Injectable()
export class YourService {
constructor(protected localStorage: LocalStorage) {}
}
Usage
let user: User = { firstName: 'Henri', lastName: 'Bergson' };
this.localStorage.setItem('user', user).subscribe(() => {});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…