NgZone / Angular2 / Ionic2 TypeError:无法读取未定义的属性“运行”

帕特里克1870

我收到此错误TypeError:无法读取Subscriber.js:229中未定义的属性“运行”,并且不知道为什么-在ionic beta 10中,此代码可以正常工作……在11中则不能。

import {Component, NgZone} from '@angular/core';
import {NavController} from 'ionic-angular';

declare var io;

@Component({
  templateUrl: 'build/pages/home/home.html'
})    
export class HomePage {
    static get parameters() {
        return [NgZone];
    }

    zone: any;
    chats: any;
    chatinp: any;
    socket: any;

constructor(public navCtrl: NavController, ngzone) {
    this.zone = ngzone;
    this.chats = [];
    this.chatinp ='';
    this.socket = io('http://localhost:3000');
    this.socket.on('message', (msg) => {
        this.zone.run(() => {
            this.chats.push(msg);
        });
    });
}

send(msg) {
    if(msg != ''){
        this.socket.emit('message', msg);
    }
    this.chatinp = '';
   }
}
塞巴费雷拉斯

而不是像这样注入它:

static get parameters() {
  return [NgZone];
}

您为什么不这样做:

import { Component, NgZone } from "@angular/core";

@Component({
  templateUrl:"home.html"
})
export class HomePage {

  public chats: any;

  constructor(private zone: NgZone) {

    this.chats = [];
    let index: number = 1;

    // Even though this would work without using Zones, the idea is to simulate
    // a message from a socket.
    setInterval(() => { this.addNewChat('Message ' + index++); }, 1000);
  }

  private addNewChat(message) {
    this.zone.run(() => {
        this.chats.push(message);
    });
  }
}

我在中添加private zone: NgZone了一个参数constructor,然后可以使用像这样run()zone变量来使用该方法

this.zone.run(() => {
  // ... your code
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Angular2:无法读取未定义的属性“名称”

angular2:错误:TypeError:无法读取未定义的属性“ ...”

运行npm测试(Angular 2单元测试)后无法读取未定义的属性“ subscribe”

angular2形式-无法读取未定义的属性替换

Angular2:TypeError:无法读取未定义的属性“ Symbol(Symbol.iterator)”

Angular 2异常:TypeError:无法读取未定义的属性“ annotations”

Angular2无法读取未定义的属性“ push”

无法读取未定义[Angular2]的属性“ isStopped”

Google Picker API + Angular2收到未捕获的TypeError:无法读取未定义的属性'handleAuthResult'

TypeError:无法读取未定义角度2的属性“ http”

Angular 2-未捕获(承诺):TypeError:无法读取未定义的属性“ title”

Angular 2-TypeError:无法读取未定义的属性“ apply”

Angular 2 TypeError:无法读取未定义的属性“ toUpperCase”

无法读取未定义angular2的属性

无法读取原生脚本angular2中未定义的属性全局数组

使用Angular2中的动态组件时出现错误“错误TypeError:无法读取未定义的属性'createComponent'”

TypeError:在angular2中执行单元测试时,无法读取未定义故障的属性“快照”

Typescript / Angular2 TypeError:无法读取未定义的属性

TypeError:无法读取angular2中未定义的属性“ 0”以在datepicker中使用索引

TypeError:无法读取未定义的属性“ upgradeElements”,问题2是TypeError:无法读取未定义的属性“ downgradeElements”

TypeError:无法读取未定义的属性“ body2”

Webpacked Angular2应用程序TypeError:无法读取未定义的属性“ getOptional”

Angular 2 rc1 TypeError:无法读取未定义的属性“注释”

Angular2(RC-4):无法读取未定义的属性“ pathsWithParams”

Angular - Ionic 2 - 无法读取未定义的属性“订阅”

(Angular2 和 Typescript)Angular 的 DatePicker 无法读取未定义的属性“切换”

Angular / DataTables:TypeError:无法读取未定义的属性“aDataSort”与 2 个 API 调用

Angular4 ng2-charts TypeError:无法读取未定义的属性“长度”

Angular 7 + Ionic 4 构建失败“TypeError:无法读取未定义的属性(读取'种类')”