The use of RxJS Subjects is common, but not without problems. In this lesson we will see how they can be usually safely replaced with plain Observables.

Check the follow code:

const click$ = new Rx.Subject();

document.addEventListener('click', function(e) {
click$.next(e)
}); click$.subscribe(function(v) {
console.log(v)
});

One problem for this is that 'click$' become a global variable, not easy for maintance.

Not so sure how it will impact Angular, because Angular use component anyway, the subject only available to the component, maybe have low impact.

But let's see if you are not using Angular, how to conver a Subject to Observable.

const click$ = Rx.Observable.create(function(observer) {
const handler = (e) => {
observer.next(e)
}; document.addEventListener('click', handler); return function unsubscribe(){
document.removeEventListener('click', handler)
} }); const subscription = click$.subscribe(function (ev) {
console.log(ev.clientX);
}); setTimeout(function () {
subscription.unsubscribe();
}, );

最新文章

  1. 苹果手机不支持click文字 需要添加 cursor:pointer 才能 识别可以点击
  2. USACO翻译:USACO 2014 FEB SILVER 三题
  3. C#调用斑马打印机打印条码标签(支持COM、LPT、USB、TCP连接方式和ZPL、EPL、CPCL指令)【转】
  4. ajax读取XML文本(如读取城市)
  5. Octopus系列之各个页面调用示例
  6. C++ MFC获取软件运行目录 (包含软件名)
  7. Hello WPF!
  8. windows 8.1 MessageDialog
  9. MonoBehaviour
  10. Javascript之响应式相册
  11. mysql命令行导出导入数据库
  12. [AngularJS] 5 simple ways to speed up your AngularJS application
  13. 多目标遗传算法 ------ NSGA-II (部分源码解析)README 算法的部分英文解释
  14. 伪元素”:after” , “:before"
  15. WC2006水管局长(加强)
  16. poj 1873 凸包+枚举
  17. vs2017 +CUDA 9.0配置
  18. CF1131D Gourmet choice
  19. AX3298添加新sensor
  20. WPFToolkit DataGrid 使用介绍zz

热门文章

  1. RecyclerView具体解释
  2. MyEclipse 2016 安装/破解
  3. ORA-16055: FAL request rejected
  4. slice深拷贝数组
  5. [Angular] Use :host-context and the ::ng-deep selector to apply context-based styling
  6. Android提示版本号更新操作流程
  7. Linux经常使用命令(七) - cp
  8. 整理wmic使用,不重启变环境变量 .
  9. HTTP请求报文、响应报文
  10. TC Hangs when using quick search extended on win10 (1703)