Skip to content

Instantly share code, notes, and snippets.

Avatar

Cyrille Tuzi cyrilletuzi

View GitHub Profile
View app.module.ts
import { LocalStorageModule } from '@ngx-pwa/local-storage';
@NgModule({
imports: [
BrowserModule,
LocalStorageModule
]
})
export class AppModule {}
View your.service.ts
import { LocalStorage } from '@ngx-pwa/local-storage';
@Injectable()
export class YourService {
constructor(protected localStorage: LocalStorage) {}
ngOnInit() {
this.localStorage.setItem('lang', 'fr').subscribe(() => {
View systemjs.config.js
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'
}
});
View your.service.ts
this.localStorage.setItem('color', 'red').subscribe(() => {
// Done
}, () => {
// Error
});
View your.service.ts
let user = { firstName: 'Henri', lastName: 'Bergson' };
this.localStorage.setItem('user', user).subscribe(() => {}, () => {});
View your.service.ts
this.localStorage.removeItem('user').subscribe(() => {}, () => {});
View your.service.ts
this.localStorage.clear().subscribe(() => {}, () => {});
View your.service.ts
this.localStorage.getItem('notexisting').subscribe((data) => {
data; // null
}, () => {
// Not called
});
View your.service.ts
this.localStorage.getItem('user').subscribe((user) => {
if (user != null) {
user.firstName;
}
}, () => {});
View your.service.ts
this.localStorage.getItem('color').subscribe((color: string) => {
color;
}, () => {});