Angular 2:无法读取未定义的属性"push" [英] Angular 2 : cannot read property 'push' of undefined

查看:231
本文介绍了Angular 2:无法读取未定义的属性"push"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Angular 2应用程序中,我有一个功能:

In my Angular 2 application I have a function :

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秒从服务器收到一次通知,并尝试将它们推送到一个数组,但是有一个方法:

In this function, I get notification from the server every 5 seconds and try to push them to an array, but a have this:

错误app.module.ts:104错误:TypeError:无法读取未定义(...)的属性"push"

有什么建议吗?

推荐答案

更改

notification : Array<any>;

notification : Array<any> = [];

这篇关于Angular 2:无法读取未定义的属性"push"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆