Events 当信号器调用客户端时,如何在Angular2中抛出事件

Events 当信号器调用客户端时,如何在Angular2中抛出事件,events,typescript,signalr,angular,Events,Typescript,Signalr,Angular,在angular2应用程序中的信号器服务中,当我从服务器接收到事件时,我希望广播一个事件 @Injectable() export class SignalRService { //... private emitMe: EventEmitter<any>; constructor() { this.emitMe = new EventEmitter(); this.connection = jQuery.hubConnection(

在angular2应用程序中的信号器服务中,当我从服务器接收到事件时,我希望广播一个事件

@Injectable() export class SignalRService {

   //...
    private emitMe: EventEmitter<any>;

constructor() {
        this.emitMe = new EventEmitter();
        this.connection = jQuery.hubConnection(".../signalr/");
        this.proxy = this.connection.createHubProxy("...");
        this.wireupEvents();
        //...
    }

wireupEvents(): void {
    this.proxy.on('ItemAdded', function(data) {
            console.log('received ItemAdded' + JSON.stringify(data));
                //How to throw event here??? Like $rootScope.$emit('itemAdded', data);
                // 'this.emitMe' is not reachable here
            });
    }
}
@Injectable()导出类信号服务{
//...
私有emitter:EventEmitter;
构造函数(){
this.emitMe=neweventemitter();
this.connection=jQuery.hubConnection(“…/signalr/”);
this.proxy=this.connection.createHubProxy(“…”);
这是wireupEvents();
//...
}
wireupEvents():void{
this.proxy.on('ItemAdded',函数(数据){
log('received itemsadded'+JSON.stringify(data));
//如何在这里抛出事件???如$rootScope.$emit('itemsadded',data);
//此处无法访问“this.emitMe”
});
}
}
如何访问我初始化的EventEmitter并抛出我可以从外部订阅的事件

谢谢


关于Tenoda,您可以尝试以下方法:

constructor() {
  this.emitMe = new EventEmitter();
  //...
}

wireupEvents(): void {
  this.proxy.on('ItemAdded', (data) => {
    console.log('received ItemAdded' + JSON.stringify(data));
    let someObject = (...)
    this.emitMe.emit(someObject);
  });
}
使用箭头函数,您可以直接在回调中使用
this
关键字。此答案也可以帮助您:

希望它能帮助你,
Thierry

只需将这个分配给那个,然后使用它

private emitMe: EventEmitter<any> =  new EventEmitter();

wireupEvents(): void {
    let that = this;
    this.proxy.on('ItemAdded', function(data) {
            console.log('received ItemAdded' + JSON.stringify(data));
            that.emitMe.emit(data);
     });
  }
private emitter:EventEmitter=new EventEmitter();
wireupEvents():void{
让那=这;
this.proxy.on('ItemAdded',函数(数据){
log('received itemsadded'+JSON.stringify(data));
即.emitMe.emit(数据);
});
}
疯狂的权利;)。
的可变范围不是您需要的
,因此
没有
发射
,但通过将
分配给
现在我们有了
发射

您是如何让信号机与Angular2连接的?