RxJS当前的官方例子中
Rx.Observable.fromEvent(button,'click') .scan(count => count + 1,0) .subscribe(count => console.log(`Clicked ${count} times`));
Observable.fromEvent(this.button.nativeElement,'click') .throttleTime(1000) .scan(count => count + 1,0) .subscribe(count => console.log(`Clicked ${count} times`));
这个段代码中的 count => count + 1抛出
Operator '+' cannot be applied to types '{}'
因为angular4使用的是TypeScript,所以在当前使用的RxJS中,需要指定scan运算符中count的数据类型
因此,更改为.scan((count:number) => count + 1,0)
全部代码如下所示
Observable.fromEvent(this.button.nativeElement,'click') .throttleTime(1000) .scan((count:number) => count + 1,0) .subscribe(count => console.log(`Clicked ${count} times`));原文链接:https://www.f2er.com/angularjs/145874.html