【问题标题】:Ionic run function once only (on first time app is run)离子运行功能仅一次(在第一次运行应用程序时)
【发布时间】:2018-01-25 07:47:48
【问题描述】:

我正在尝试执行一个函数,该函数将一个数组设置为离子存储中的默认值,但是我只需要在应用程序第一次运行时(在它安装在用户手机上之后)才发生这种情况。目前每次启动应用程序时它都会触发,因为我的 component.ts 中有该功能--> platform.ready()。这是我的功能,我只是不知道该放在哪里。如您所见,我将数组设置为默认值,该数组在应用程序的其他部分中得到更新。当用户关闭应用程序并重新打开它时,数组会重置为默认值,因此我需要让函数只运行一次,即当用户第一次运行应用程序时。

  let array = [];
  array.push(
    { "username":"Name & Surname*", "email":"Email*", "cellnumber":"Cell Number*", "displayname":"Displayname*", "profilepicture":"urlpath", "projectname":"Projectname", "dateadded":"Dateadded", "notes":"Notes", "image":"Image", "taskname":"Taskname", "taskdescription":"Taskdescription", "taskimage":"Taskimage" }
  );
  this.storage.set('myStore', array); 

谢谢

【问题讨论】:

  • 检查询问 this.storage.get('myStore')!=null
  • @Eliseo 在正确的路径上只需要检查数据(如果 ( !data ))

标签: javascript angular ionic-framework ionic2 dom-events


【解决方案1】:

检查存储中是否设置了“myStore”键:

this.storage.get('myStore').then(data => {
   if ( !data ) {
       let array = [];
       array.push(yourDataObject);
       this.storage.set('myStore', array); 
   }
})
.catch(err=>{
   console.log('Your data don't exist and returns error in catch: ' + JSON.stringify(err);
});

尚未测试此代码,但它应该可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多