【问题标题】:ionic 3 read stored value native storage in function ionViewDidLoad ()ionic 3 函数ionViewDidLoad()中读取存储值原生存储
【发布时间】:2018-04-27 01:26:33
【问题描述】:

我有一个问题,我想在函数-> ionViewDidLoad() 中读取保存在Native Storage Cordova 插件中的值。

功能说明:

ionViewDidLoad () = "页面加载完毕后运行。仅此事件 每页创建一次。如果页面离开但被缓存, 那么此事件将不会在随后的查看中再次触发。这 ionViewDidLoad 事件是放置您的设置代码的好地方 页。”

我尝试执行的操作并在打开应用程序时显示一条消息。但是如果用户不希望每次应用启动时都打开,我想在函数ionViewDidLoad()中做一个条件,让它按照用户的意愿打开。

Home.ts 页面

    import { Component } from '@angular/core';
    import { NavController, AlertController } from 'ionic-angular';

    import { ParamProviders } from '../../providers/paramProviders';
    import { NativeStorage } from '@ionic-native/native-storage';

    @Component({
      selector: 'page-home',
      templateUrl: 'home.html',
      providers: [ParamProviders]
    })
    export class HomePage {

      boolBienvenue: boolean;

      constructor(public navCtrl: NavController, private alertCtrl: AlertController, private nativeStorage: NativeStorage, private paramService: ParamProviders) {
      }

      ionViewDidLoad() {
         this.boolBienvenue1()
         console.log(this.boolBienvenue);
         if ( this.boolBienvenue == true ) {
            this.demarrage();
        }
      }

     public boolBienvenue1() {
        this.nativeStorage.getItem('storage_pref')
          .then(
            data => this.boolBienvenue = data.bienvenue,
            error => console.error(error)
        )

  }

     public demarrage() {
        let alert = this.alertCtrl.create({
          title: 'Bienvenue dans HackChat !!',
          message: 'Avant de discuter avec vos amies ou vos proches, veuillez configurer l\'application en y indiquant l\'adresse de votre serveur ainsi que votre login et mot de pass dans l\'onglet Paramétres. Une fois que vous serrez connecté, vous pourrez discuter.<br />Allez y c\'est à vous !!<br /><br />Voulez vous que cette bulle d\'information s\'affiche lors de l\'ouverture de HackChat ?',
          buttons: [
            {
              text: 'Non',
              handler: () => {
                console.log('Disagree clicked');
              }
            },
            {
              text: 'Oui',
              handler: () => {
                console.log('Agree clicked');
              }
            }
          ]
        });
        alert.present();
      }
}

Parameter.ts 页面

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';

import { NativeStorage } from '@ionic-native/native-storage';

/*
  Generated class for the PeopleSearch provider.
  See https://angular.io/docs/ts/latest/guide/dependency-injection.html
  for more info on providers and Angular 2 DI.
*/
@Injectable()
export class ParamProviders {

    linkServer: string ;
    user: string;
    password: string;
    bienvenue: boolean = true;
    notification: boolean = false;
    vibration: boolean = false;

  constructor(public http: Http, private nativeStorage: NativeStorage) {
  }

public savePref(): void {
    this.nativeStorage.setItem('storage_pref', {
        linkServer: this.linkServer,
        user: this.user,
        password: this.password,
        bienvenue: this.bienvenue,
        notification: this.notification,
        vibration: this.vibration
    })
    .then(
      () => console.log(this.linkServer),
      error => console.error('Error storing item', error)
    );
}

public loadPref():void {
  this.nativeStorage.getItem('storage_pref')
    .then(
      data => {
        this.linkServer = data.linkServer;
        this.user = data.user;
        this.password = data.password;
        this.bienvenue = data.bienvenue;
        this.notification = data.notification;
        this.vibration = data.vibration;
      },
      error => console.error(error)
  );
 }
}

你能帮帮我吗 谢谢

【问题讨论】:

    标签: angularjs typescript cordova-plugins ionic3 cordova-nativestorage


    【解决方案1】:

    访问存储是一项异步作业,因此您的以下代码很可能不起作用,因为在您尝试询问您的布尔值是否已设置后,异步作业的结果已解决

    ionViewDidLoad() {
         this.boolBienvenue1() // Here you don't wait for async job
         console.log(this.boolBienvenue); // So this value might or might not be set
         if ( this.boolBienvenue == true ) {
            this.demarrage();
        }
      }
    
     public boolBienvenue1() {
        this.nativeStorage.getItem('storage_pref')
          .then(
            data => this.boolBienvenue = data.bienvenue,
            error => console.error(error)
        )
     }
    

    例如,您可以修改您的代码,如下所示:

     ionViewDidLoad() {
         this.nativeStorage.getItem('storage_pref')
          .then(
            (data) => {
                 if ( data !== null && data.bienvenue ) {
                    this.demarrage();
                 }
            }
            error => console.error(error)
        )
     }
    

    P.S.:与您的问题无关,但如果可以的话,在不需要添加== true 的条件下测试布尔值是否为真,布尔值本身就足够了。我的意思是:

     if (something == true)
    

    应该写

     if (something)
    

    【讨论】:

    • 非常感谢您的帮助和建议。
    猜你喜欢
    • 2018-04-23
    • 1970-01-01
    • 2018-03-10
    • 1970-01-01
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多