NgZone / Angular2 / Ionic2 TypeError:无法读取未定义的属性’run’

前端之家收集整理的这篇文章主要介绍了NgZone / Angular2 / Ionic2 TypeError:无法读取未定义的属性’run’前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我收到此错误TypeError:无法在Subscriber.js中读取未定义的属性’run’:229并且不知道为什么 – 在离子beta 10中这个代码工作正常…在11中没有.
  1. import {Component,NgZone} from '@angular/core';
  2. import {NavController} from 'ionic-angular';
  3.  
  4. declare var io;
  5.  
  6. @Component({
  7. templateUrl: 'build/pages/home/home.html'
  8. })
  9. export class HomePage {
  10. static get parameters() {
  11. return [NgZone];
  12. }
  13.  
  14. zone: any;
  15. chats: any;
  16. chatinp: any;
  17. socket: any;
  18.  
  19. constructor(public navCtrl: NavController,ngzone) {
  20. this.zone = ngzone;
  21. this.chats = [];
  22. this.chatinp ='';
  23. this.socket = io('http://localhost:3000');
  24. this.socket.on('message',(msg) => {
  25. this.zone.run(() => {
  26. this.chats.push(msg);
  27. });
  28. });
  29. }
  30.  
  31. send(msg) {
  32. if(msg != ''){
  33. this.socket.emit('message',msg);
  34. }
  35. this.chatinp = '';
  36. }
  37. }
而不是像这样注入它:
  1. static get parameters() {
  2. return [NgZone];
  3. }

你为什么不这样做:

  1. import { Component,NgZone } from "@angular/core";
  2.  
  3. @Component({
  4. templateUrl:"home.html"
  5. })
  6. export class HomePage {
  7.  
  8. public chats: any;
  9.  
  10. constructor(private zone: NgZone) {
  11.  
  12. this.chats = [];
  13. let index: number = 1;
  14.  
  15. // Even though this would work without using Zones,the idea is to simulate
  16. // a message from a socket.
  17. setInterval(() => { this.addNewChat('Message ' + index++); },1000);
  18. }
  19.  
  20. private addNewChat(message) {
  21. this.zone.run(() => {
  22. this.chats.push(message);
  23. });
  24. }
  25. }

我正在添加私有区域:NgZone作为构造函数中的参数,然后我可以通过使用区域变量来使用run()方法,如下所示:

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

猜你在找的Angularjs相关文章