我收到此错误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] 删除。
我来说两句