Angular 4 Observables鼠标坐标 [英] Angular 4 Observables mouse coordinates

查看:110
本文介绍了Angular 4 Observables鼠标坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用Angular 4仅使用Observables而不使用http服务来获得鼠标坐标X和Y.

I want to know how it's possible to get mouse coordinates X and Y using only Observables without http service using Angular 4.

推荐答案

Http模块不是RxJS可观察对象的依赖项,因此您不必使用它.实际上,Http模块本身使用RxJS来使Http操作数具有响应功能.

Http module is not a dependency for RxJS observables, so you don't have to use it. In fact, Http module itself uses RxJS to empower Http operands with reactive capabilities.

为了订阅mousemove之类的事件,您只需导入fromEventObservable并订阅其事件发射器.这样的事情应该起作用:

In order to subscribe to events like mousemove, you would simply import fromEvent and Observable and subscribe to its event emitter. Something like this should work:

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/fromEvent';

Observable.fromEvent(document.body, 'mousemove').subscribe(e => {
  console.log(e.pageX, e.pageY)
})

当然,请使用适当的事件源(而不是document.body).

Of course, use the appropriate event source (instead of document.body).

这篇关于Angular 4 Observables鼠标坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆