在rxjs 5.1中处理同一DOM节点上的多个事件的最佳方法是什么?
fromEvent($element, 'event_name') 但我一次只能指定一个事件.
我想处理scroll wheel touchmove touchend事件.
我知道一点BaconJS,但现在我正在尝试通过创建"用户输入..."指示器来学习RxJS.它非常简单,可以用两个简单的规则来解释:
我不确定这是否正确,但我到目前为止创建了两个流:
0每秒发出一次.1为每个事件发出一个.然后我将它们合并在一起,然后只需点击结果.如果是a 1,那么我会显示指标.如果它是a 0,那么我隐藏指标.
这是看起来像:
const showTyping = () =>
$('.typing').text('User is typing...');
const showIdle = () =>
$('.typing').text('');
// 1 second heartbeats are mapped to 0
const heartbeat$ = Rx.Observable
.interval(1000)
.mapTo(0);
// user typing events are mapped to 1
const input$ = Rx.Observable
.fromEvent($('#input'), 'input')
.mapTo(1);
// we merge the streams together
const state$ = heartbeat$
.merge(input$)
.do(val => val === 0 ? showIdle() : showTyping())
.subscribe(console.log); …Run Code Online (Sandbox Code Playgroud) 我需要/希望在forkJoin中使用六个以上的参数。当前,基于对另一个相关问题的答案,似乎无法向forkJoin发送超过6个参数。
但是,根据官方文档,它说:“ forkJoin是一个运算符,它接受可作为数组或直接作为参数传递的任意数量的Observable。”
forkJoin-官方文档
好吧,我正在这样做,并且出现错误TS2322:类型'foo'无法分配给类型'bar []'。
在我的研究中,我还发现,如果您有承诺返回不同的类型,最好不要将参数作为数组发送,因为这样会将类型转换为所有相同的类型。- 来源
这是我的代码。我正在使用最新版本的Typescript和Angular 4。
ngOnInit() {
this.spinner.show();
Observable.forkJoin(
this.loadParams(), // Returns an Observable<Object>
this.service.getPromiseType1(), // The rest return Observable<Array>
this.service.getPromiseType2(),
this.service.getPromiseType3(),
this.service.getPromiseType4(),
this.service.getPromiseType5(),
this.service.getPromiseType6(),
).finally(() => this.spinner.hide())
.subscribe(
([param, promise1, promise2, promise3, promise4, promise5, promise6]) => {
this.job = job;
this.value1 = promise1;
this.value2 = promise2;
this.value3 = promise3;
this.value4 = promise4;
this.value5 = promise5;
this.value6 = promise6;
}, (error) => {errorHandlingFunction}
});
Run Code Online (Sandbox Code Playgroud)
如果我删除任何单个参数,以便将六个参数传递给forkJoin,它就可以正常工作。所以我的问题是,在我想一次调用一次加载对象可观察对象和后续数组可观察对象的情况下,还有另一种方法吗?这是forkJoin的错误,因为官方文档说它应该能够接受任意数量的Observables?
我试图创建一个类型为Observable的数组,并在forkJoin中使用array.forEach(),但是它抱怨返回的是void类型。无论如何,这似乎是一种不灵活的方式。