Ionic - Angular persist state w/ ionicstorage #357
-
As per title, I'm implementing this library in an Ionic - Angular project. With Angular I have to use a different strategy to implement ionic storage -> https://github.com/ionic-team/ionic-storage#with-angular Any idea? Tnx in advance to all :) My solution for now is to create a import { Storage } from '@ionic/storage';
import { StateStorage } from '@ngneat/elf-persist-state';
const storage = new Storage();
storage.create();
export const getStorageItem = async <T extends Record<string, any>>(key: string): Promise<T | undefined> =>
await storage.get(key);
export const setStorageItem = async (key: string, value: Record<string, any>): Promise<void> => {
await storage.set(key, value);
};
export const removeStorageItem = async (key: string): Promise<void> => {
await storage.remove(key);
};
export const getStorageKeys = async (): Promise<string[]> => storage.keys();
export const clearStorage = async (): Promise<void> => storage.clear();
export const ionicStateStorage: StateStorage = {
getItem: getStorageItem,
setItem: setStorageItem,
removeItem: removeStorageItem
}; and import it in import '#core/services/storage/storage.service';
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['app.component.scss']
})
export class AppComponent {
constructor() {}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Yes, that's the way to do it. |
Beta Was this translation helpful? Give feedback.
-
Complete implementation could be found HERE |
Beta Was this translation helpful? Give feedback.
Yes, that's the way to do it.