NgZone/Angular2/Ionic2 TypeError: невозможно прочитать свойство «запуск» неопределенного

Я получаю эту ошибку TypeError: Cannot read property 'run' of undefined в 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 = '';
   }
}

person Patrick1870    schedule 13.08.2016    source источник


Ответы (1)


Вместо того, чтобы вводить его следующим образом:

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
});
person sebaferreras    schedule 14.08.2016