2017-06-29 12 views
1

私のアプリが初めて実行されたときに、たくさんのコマンドを実行しようとしています。私はいくつかのコードを見つけましたが、それらのどれも私のために働くようです。ここで私が今持っているものです。初めてアプリを実行しているときにイオンを使って何かを実行するには?

var applaunchCount = this.storage.get('launchCount'); 
    console.log(applaunchCount); 

    if(applaunchCount){ 
      this.hello="succesive"; 
    }else{ 
    storage.set('launchCount','1'); 
     this.hello="first"; 
    } 

私はハローの値は常に「succesive」のまま私のAndroidデバイス上でそれを試してみました。 編集:あなたは、データがそれを使用する前に準備ができるまで待たなければならないので、 最初のページですhome.tsファイル

import { Component } from '@angular/core'; 
import { NavController } from 'ionic-angular'; 
import { FormBuilder, Validators } from '@angular/forms'; 
import {cloths} from '../defaults.component'; 
import { Storage } from '@ionic/storage'; 

@Component({ 
    selector: 'page-home', 
    templateUrl: 'home.html' 
}) 


export class HomePage { 
hello : string; 
clothes=cloths; 
l : string ; 
public loginForm = this.fb.group({ 
    new: ["", ] 

    }); 


    constructor(public navCtrl: NavController, public fb:FormBuilder, public storage:Storage) { 
    var applaunchCount = this.storage.get('launchCount'); 
    var app : string; 
    console.log(applaunchCount); 

    if(applaunchCount){ 
    //This is a second time launch, and count = applaunchCount 
      this.hello="succesive"; 
    }else{ 
    //Local storage is not set, hence first time launch. set the local storage item 
    storage.set('launchCount','1'); 
     this.hello="first"; 
    //Do the other stuff related to first time launch 
    } 

    } 
    buttonLogin(){ 

    this.clothes.push(this.loginForm.get('new').value); 


    } 


} 
+0

'applaunchCount'の値は何ですか – Duannx

+0

app.component.tsのコンストラクタ内にコードを追加します – Gowtham

+0

@Duannx logステートメントは何も表示しません。その空の –

答えて

0

ストレージからデータを取得するには、async操作です:

constructor(public navCtrl: NavController, public fb:FormBuilder, public storage:Storage) { 

    let app : string; // <- You can use let instead of var here 

    this.storage.get('launchCount').then(applaunchCount => { // <- Wait for the data to be ready 

     // Now the data from the storage is ready! 

     console.log(applaunchCount); 

     if(applaunchCount) { 
      // This is a second time launch, and count = applaunchCount 
      this.hello = "succesive"; 
     } else { 
      // Local storage is not set, hence first time launch. set the local storage item 
      storage.set('launchCount','1'); 

      this.hello = "first"; 

      // Do the other stuff related to first time launch 
     } 
    }); 

} 
関連する問題