javascript - 我想用 rxjs observable 替换 addEventListener

标签 javascript html css rxjs observable

我正在尝试通过使用 rxjs 单击按钮通过 XHR 获取数据

document.getElementById('button')
    .addEventListener('click', function() {
        console.log("button clicked");
    });

最佳答案

fromEvent(document.getElementById('button'), 'click').pipe(
  switchMap(_ => ajax('yourEndpointUrl'))
).subscribe(
  res => { // do stuff with response },
  err => { // handle error }
);

关于javascript - 我想用 rxjs observable 替换 addEventListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58616719/

相关文章:

javascript - Jquery 切换多个类

javascript - Node.js 中的面向对象设计

javascript - jQuery 数据表 1.10 : Which column is it sorted on right now?

javascript - html <input type ="text"/> 仅包含最小值和最大值的数字

CSS 将 div 发送到其他 div 的底部;底部 :0 isn't working

javascript - 为什么域在谷歌分析中被散列?

html - 如何创建一个 DIV,它在 :hover (for desktop) and on tap (for touch devices) using CSS? 上下拉另一个 DIV

javascript - 固定标题元素在窗口上移动减少

html - 如何避免 Safari 将 -apple-system-blue 添加到我的按钮

html - CSS div 相对于其他 div 的定位