我第一次执行我的应用程序时试图运行一堆命令。我发现了几件代码,但它们似乎都不适合我。这是我现在拥有的:
var applaunchCount = this.storage.get('launchCount');
console.log(applaunchCount);
if(applaunchCount){
this.hello="succesive";
}else{
storage.set('launchCount','1');
this.hello="first";
}
我在我的Android设备上尝试了它,Hello的价值总是"成功"。编辑 :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);
}
}
从存储中获取数据是一个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
}
});
}