简体   繁体   English

可观察到的Angular2无法正常工作

[英]Angular2 observable not working

I am using peerjs library to build a chat. 我正在使用peerjs库来建立聊天。 I'm trying to do an observable to trigger an event every time I get a new message. 每当我收到新消息时,我都试图做一个可观​​察的事件来触发事件。

this.data = new Observable(observer => {
                   this.peer.on('connection', (data) => {
                    alert("TRIGGER");
                    observer.next(data);   
                });
             })


             this.peerMessages = this.data.subscribe(message => {
                alert(message);
            });

I can see the first alert but the second one never triggers. 我可以看到第一个警报,但第二个警报从未触发。 I don't get any error from the browser console. 我没有从浏览器控制台中得到任何错误。 What am I doing wrong? 我究竟做错了什么?

Im trying to use an observer because instead of alert(message) I want to change a variable from the angular component. 我试图使用观察器,因为我想从角度组件更改变量而不是alert(message)。

The code looks good, but make sure you import everything you need. 该代码看起来不错,但请确保导入所需的所有内容。 For example, if using rxjs : 例如,如果使用rxjs

import { Observable, ... } from 'rxjs/Observable';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM