2016-12-23 12 views
1

私のアプリケーション内でIonic Storageをどのように使用できますか?私は次のことを試しましたが、私が読むことを試みるとき、私はいつも値として[object Object]を返します。 1つのクラスにget/setメソッドがある場合に機能しますが、私はプロジェクトを構造化し、ストレージ部分を外部に委譲したいと考えています。Ionic Storageは未定義の値を提供します

@Component({ 
     selector: 'page-login', 
     templateUrl: 'login.html' 
    }) 
    export class LoginPage { 

     constructor(public testData: TestData) {} 

     onLogin(form) { 

     if (form.valid) { 
      this.testData.setTestParam("abc"); 
      console.log("Stored: " + this.testData.getTestParam()) ; 
      // delivers => Stored: [object Object] 
     } 
    }  

TESTDATAクラス

@Injectable() 
export class TestData { 

constructor(public storage : Storage) {} 

setTestParam(testparam) 
{ 
    this.storage.set('test_param', testparam); 
} 

getTestParam(){ 
    return this.storage.get('test_param').then((value) => { 
     return value; 
    }); 
} 
} 
+0

あなたのデバッグコンソールにアクセスし、中にあなたの[アプリケーション]タブに、データがWebSQLの下に保存されているか、どちらかのドライバは、私がしなければならない – misha130

答えて

1

あなたgetTestParam()リターンの約束ではない値。 あなたはgetTestParam約束の代わりに、通常の値を返す

this.testData.getTestParam().then(data=>{ 
    console.log(data); 
}) 
+0

を使用した方法を見クロムである場合これは私のLoginPageクラスにありますか? – CallMeBronxy

+0

yes .. 'console.log(" Stored: "+ this.testData.getTestParam());" –

+0

ありがとうございます。これは正しい方法ですか、他の何かをお勧めしますか? – CallMeBronxy

0

を実行して値を読み取ることができます。 Promiseから戻り値を読み取るには、thenメソッドを使用し、catchメソッドを使用してエラーを処理する必要があります。

1

このご希望があれば、お手伝いします。

@Component({ 
     selector: 'page-login', 
     templateUrl: 'login.html' 
    }) 
    export class LoginPage { 

     constructor(public testData: TestData) {} 

     onLogin(form) { 
     if (form.valid) { 
      this.testData.setTestParam("abc"); 
      this.testData.getTestParam().then((value: any) => { 
      console.log(value); 
      }); 
      }}} 

TESTDATAクラス

@Injectable() 
export class TestData { 

constructor(public storage : Storage) {} 

setTestParam(testparam) 
{ 
    this.storage.set('test_param', testparam); 
} 

getTestParam(){ 
     return new Promise((resolve, reject) => { 
      this.storage.get('test_param').then((value) => { 
       resolve(value); 
      }); 
     }); 
} 
} 
関連する問題