2017-07-28 80 views
0

我刚开始使用Ionic框架。到现在为止,我已经一起走了。我的问题是,一个数组在运行时被忽略,我不知道为什么。 我很难过。我认为,对于这个问题,最好先发布我的代码。Array在运行时莫名其妙地被取消

export interface Days 
{ 
    name:string; 
} 

@Component({ 
    selector: 'page-home', 
    templateUrl: 'home.html' 
}) 
export class HomePage 
{ 
    days = [] as Days[]; 
    constructor(public navCtrl: NavController, public alertCtrl: AlertController, public navParams: NavParams) 
    { 

    } 

    presentDayDialog(): void 
    { 
    let alert = this.alertCtrl.create({ 
     title: 'Add new day', 
     message: 'Enter a name for the new day', 
     inputs: [ 
     { 
      name: 'day', 
      placeholder: 'Day' 
     }, 
     ], 
     buttons: [ 
     { 
      text: 'Cancel', 
      handler:() => { 
      console.log('Cancel clicked'); 
      } 
     }, 
     { 
      text: 'Save', 
      handler: data => { 
      this.addDays(data.day); 
      } 
     } 
     ] 
    }); 
    alert.present(); 
    } 

    addDays(rname): void 
    { 
    this.days.push({name: rname}); 
    } 

    itemTapped(Days) 
    { 
    this.navCtrl.push(RoutinePage, { 
     pageName: Days.name 
    }); 
    console.log("The page name was "+Days.name); 
    } 
} 

export interface Routine 
{ 
    name:string; 
    weight: number; 
    reps: number; 
} 

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

export class RoutinePage 
{ 
    routines = [] as Routine[]; 
    public pageName; 

    constructor(public navCtrl: NavController, public toastCtrl: ToastController, public alertCtrl: AlertController, public platform: Platform, public storage: Storage, public navParams: NavParams) 
    { 
    console.log('PRE CONSTRUCTOR ' + this.routines); 

    this.pageName = navParams.get("pageName"); 
    this.getRoutines(); 
    console.log('POST CONSTRUCTOR ' + this.routines); 
    } 

    //Sets the routines to storage 
    setRoutines() 
    { 
    console.log('ROUTINES ARRAY before setRoutine() '+ this.routines); 
    this.storage.set(this.pageName, this.routines); 
    console.log('ROUTINES ARRAY after setRoutine() '+ this.routines); 
    } 

    //Gets the routines from storage, this gets executed at the construction of this page so it isn't empty at the start 
    getRoutines() 
    { 
    console.log('ROUTINES ARRAY before getRoutine() '+ this.routines); 
    this.routines = [{name: 'Nigger', weight: 0, reps: 0}]; 
    this.storage.get(this.pageName).then((data) => { 
     this.routines = data; 
    }); 
    console.log('ROUTINES ARRAY after getRoutine() '+ this.routines); 
    } 

    //Adds a new routine and saves it to storage 
    addRoutine(rname): void 
    { 
    console.log('ROUTINES ARRAY before addRoutine() '+ this.routines); 
    this.routines.push({name: rname, weight: 0, reps: 0}); 
    console.log('ROUTINES ARRAY after addRoutine() ' + this.routines); 
    this.setRoutines(); 
    } 

    //Presents the dialog for adding a new routine on FAB-button-press, calls addRoutine function 
    presentRoutineDialog(): void 
    { 
    let alert = this.alertCtrl.create({ 
     title: 'Add new routine', 
     message: 'Enter a name for the new routine', 
     inputs: [ 
     { 
      name: 'routine', 
      placeholder: 'Routine' 
     }, 
     ], 
     buttons: [ 
     { 
      text: 'Cancel', 
      handler:() => { 
      console.log('Cancel clicked'); 
      } 
     }, 
     { 
      text: 'Save', 
      handler: data => { 
      console.log('ROUTINES ARRAY AFTER SAVE BUTTON PRESS:' +this.routines); 
      this.addRoutine(data.routine); 
      } 
     } 
     ] 
    }); 
    console.log('ARRAY BEFORE ALERT: ' + this.routines); 
    alert.present(); 
    console.log('ARRAY AFTER ALERT: ' + this.routines);  
    } 
} 

所以,这就是我想这应该工作。我将项目(天)添加到“第一个”页面上的列表中。当我点击该项目时,我打开第二页并传递“日”的名称,然后成为第二页的名称。我也使用这个名称作为key-name来将这个数组存储在存储中。因此,每个页面都有自己的存储空间,页面中充满了构建信息。

问题: 在运行时,例程数组莫名其妙地被忽略,除非我点击的第一页的日期名称为空。这可以和我的hacky感觉存储系统有点关系,但我无法想象究竟是什么罪魁祸首。 这些都是控制台日志,我发现还挺有意思:

The page name was Upper Body home.ts:63:4 
PRE CONSTRUCTOR routine.ts:28:4 
ROUTINES ARRAY before getRoutine() routine.ts:46:4 
ROUTINES ARRAY after getRoutine() [object Object] routine.ts:51:4 
POST CONSTRUCTOR [object Object] routine.ts:32:4 
ARRAY BEFORE ALERT: null routine.ts:91:4 
ARRAY AFTER ALERT: null routine.ts:93:4 
ROUTINES ARRAY AFTER SAVE BUTTON PRESS:null routine.ts:85:12 
ROUTINES ARRAY before addRoutine() null 

所以,后立即构造器“已完成”,该阵列是突然空。这是为什么?我没有看到什么?我诚实地在这里结束了我的智慧,我希望有人能够帮助。

+1

这里'data'的值是什么:'this.routines = data;'? – robbannn

+0

我怀疑@robbannn是否正确指出了问题的根源。我会添加一行'console.log('POST CONSTRUCTOR'+ this.routines);'当你认为它没有运行时运行。具体来说,它不会在'getRoutines'完成后显示'this.routines'的值,而是显示值之前的值,因为该函数是异步的 –

+0

可能的重复[如何从Observable/http/async调用返回响应在angular2?](https://stackoverflow.com/questions/43055706/how-do-i-return-the-response-from-an-observable-http-async-call-in-angular2) – Alex

回答

0

好的,我在SO的帮助下修复了它。 从storage.get('ArrayKey')返回的诺言是null,因为我的应用试图获取的存储不存在。 我所做的是检查从storage.get('ArrayKey')返回的承诺是否为空。 如果是这样,我让storage.set('ArrayKey')存储数组作为我初始化它的值,这是数组[] - >空,但不是null。 然后,我打电话给storage.get('ArrayKey')。这现在成功了,因为Array现在是为空,但不是null

有了这个标题,我怀疑任何有同样问题的人都会发现这一点,但也许他/她仍然会找到它。欢呼并感谢您的帮助。

相关问题