Categorias
iiiiii
guardar datos locales
Descripcion
Guardar informacion en la memoria del dispositivo
Nota
DOcumentacion oficial: https://ionicframework.com/docs/v6/vue/storage
Documentacion de girhub : https://github.com/ionic-team/ionic-storage
1. instalar el plugin :npm install @ionic/storage-angular
2. ir al modulo principal de angular (appModule) donde se va implementar y se debe importar.
3. crear un servicio con la estructura ya en el ejemplo (StorageService)
html
local
typescript
//** APPMODULE INICIO Importando el localstorage modulo******************************************/
import { IonicStorageModule } from '@ionic/storage-angular';
@NgModule({
imports: [
IonicStorageModule.forRoot()
]
})
export class AppModule { }
//** APPMODULE FIN ********************************************************************************/
/***********************Mi serivicio Inicio********************************/
import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage-angular';
@Injectable({
providedIn: 'root'
})
export class StorageService {
private _storage: Storage | null = null;
private _articleLocalStorage: Article []=[];
private _cantidadLocalstorage :number = 0;
constructor(private storage: Storage) {
this.init();
}
async init() {
const storage = await this.storage.create();
this._storage = storage;
this.cargarDatosGuardados(); //se guardan los favoritos en una variable para tenerlos siempre
}
// Create and expose methods that users of this service can
// call, for example:
public set(key: string, value: any) {
this._storage?.set(key, value);
}
async cargarDatosGuardados(){
const cantidad = await this.mistorage.length();
if(cantidad!==undefined){
this._articleLocalStorage = await this.mistorage.get('Article');
}
}
get getLocalData(){ //ponemos publico los datos, la funcion get ara que angular mantenga pendiente de los datos que cambien dentro de esta variable.
return [...this._articleLocalStorage];
}
}
/***********************Mi serivicio Fin********************************/