#ionic-framework
Вопрос:
Я использую angular в своем ionic, и я следил за всем, основываясь на этих документах, поэтому я использую «@ionic/storage-angular», но моя проблема в том, что при обновлении данные удаляются. Ниже приведен мой код.
//приложение.модуль
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';
import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { IonicStorageModule } from '@ionic/storage-angular';
@NgModule({
declarations: [AppComponent],
entryComponents: [],
imports: [
BrowserModule,
IonicModule.forRoot(),
IonicStorageModule.forRoot(),
AppRoutingModule,
FormsModule,
ReactiveFormsModule,
HttpClientModule
],
providers: [
{
provide: RouteReuseStrategy,
useClass: IonicRouteStrategy,
},
],
bootstrap: [AppComponent]
})
export class AppModule {}
//хранение.обслуживание
import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage-angular';
@Injectable({
providedIn: 'root'
})
export class StorageService {
private _storage: Storage | null = null;
constructor(private storage: Storage){
this.init();
}
async init() {
// https://github.com/ionic-team/ionic-storage
// If using, define drivers here: await this.storage.defineDriver(/*...*/);
const storage = await this.storage.create();
this._storage = storage;
}
public set(key: string, value: any) {
this._storage?.set(key, value);
}
public get(key){
return this._storage?.get(key);
}
public clear(){
this._storage.clear();
}
}
//вот как я воспользовался сервисом
await this.storageService.set('loginUser',{id: user.id,userName: user.first_name});
Комментарии:
1. из-за этой строки:
const storage = await this.storage.create();
каждый раз, когда ваше приложение загружает эту строку, создается новое хранилище и удаляются все предыдущие данные.2. так что же делать? @Наджамуссакиб
3. @top.eng Вы используете cordova или конденсатор??
4. @top.eng вы нашли какое-либо решение по этому вопросу ?