2016-11-17 42 views
3

财产“推”在我的角2应用程序,我有一个函数:角2:不能读取的不确定

notification : Array<any>; 
...... 
...... 
getNotification() { 
    return setTimeout(() => { 
     this.AppbankService.notify(this.user.NameId) 
    .subscribe(
     (response) => { 
      if (response.status === 200) { 
      this.notifications.push(response.json()); 
      console.log(typeof(this.notifications)); 
       } 
      this.getNotification(); 
     } 
    ) 
    },5000)} 

在这个函数中,我得到通知,从服务器每5秒,并试图将它们推到一个数组,但有一个:错误app.module.ts:104错误:TypeError:无法读取属性'推'的undefined(...)

任何建议吗?

回答

8

变化

notification : Array<any>; 

notification : Array<any> = []; 
+0

thx它适合我,但你能告诉我为什么我必须这样做吗?类型不够? –

+2

不,类型只指定可以在此属性中存储哪种类型的值,但它不具有值。用'= []'我们用一个有'.push()'方法的空数组初始化它。 –

+0

适合我..大 –

0

我不得不推串消息的同样的问题,我的问题已经通过下面的代码解决。

messages: Array<string> = []; 
    add(message: string): void { 
    this.messages.push(message); 
}