Skip to content

Instantly share code, notes, and snippets.

View cyrilletuzi's full-sized avatar

Cyrille Tuzi cyrilletuzi

View GitHub Profile
import { LocalStorageModule } from '@ngx-pwa/local-storage';
@NgModule({
imports: [
BrowserModule,
LocalStorageModule
]
})
export class AppModule {}
import { LocalStorage } from '@ngx-pwa/local-storage';
@Injectable()
export class YourService {
constructor(protected localStorage: LocalStorage) {}
ngOnInit() {
this.localStorage.setItem('lang', 'fr').subscribe(() => {
System.config({
map: {
'@angular/core': 'node_modules/@angular/core/bundles/core.umd.js',
'angular-async-local-storage': 'node_modules/angular-async-local-storage/bundles/async-local-storage.umd.js'
}
});
this.localStorage.setItem('color', 'red').subscribe(() => {
// Done
}, () => {
// Error
});
let user = { firstName: 'Henri', lastName: 'Bergson' };
this.localStorage.setItem('user', user).subscribe(() => {}, () => {});
this.localStorage.removeItem('user').subscribe(() => {}, () => {});
this.localStorage.clear().subscribe(() => {}, () => {});
this.localStorage.getItem('notexisting').subscribe((data) => {
data; // null
}, () => {
// Not called
});
this.localStorage.getItem('user').subscribe((user) => {
if (user != null) {
user.firstName;
}
}, () => {});
this.localStorage.getItem('color').subscribe((color: string) => {
color;
}, () => {});