【发布时间】:2019-04-30 21:15:11
【问题描述】:
我正在尝试在 Angular 4 中使用过滤器,这是我的代码
import { Component, OnInit } from '@angular/core';
import { range } from 'rxjs/observable/range';
import { map, scan, filter, tap } from 'rxjs/operators';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent implements OnInit {
ngOnInit(){
this.rxjs();
}
rxjs(){
const source$ = range(1, 10);
source$.pipe(
filter(n => n % 2 !== 0),
tap(n => console.log('filtered value: ' + n)),
map(n => n * n),
tap(n => console.log('squared value: ' + n)),
scan((acc,s) => acc + s, 0)
)
.subscribe(v => console.log(`sum of squared values : ${v}`));
}
}
这是我使用 ng --version 命令发现的 Angular 版本
@angular/cli: 1.4.10
node: 8.9.1
os: win32 x64
@angular/animations: 4.4.7
@angular/common: 4.4.7
@angular/compiler: 4.4.7
@angular/core: 4.4.7
@angular/forms: 4.4.7
@angular/http: 4.4.7
@angular/platform-browser: 4.4.7
@angular/platform-browser-dynamic: 4.4.7
@angular/router: 4.4.7
@angular/cli: 1.4.10
@angular/compiler-cli: 4.4.7
@angular/language-service: 4.4.7
typescript: 2.3.4
但是当我编译时,我得到一个这样的错误
E:/angular-mock/routes/src/app/app.component.ts (19,19) 中的错误: 算术运算的左侧必须是“any”类型, 'number' 或枚举类型。
谁能帮我解决我在这方面做错了什么
【问题讨论】:
-
哪个版本的 RxJS?我无法重现这个。
-
安装 rxjs-compat 就可以了。
-
看起来有点混合了 RxJS 5 和 RxJS 6 语法。我猜这就是它不起作用的原因。例如,RxJS 运算符的导入语句使用 RxJS 6 语法。但是您使用的是 RxJS 5。有关更多信息,请参阅此链接:academind.com/learn/javascript/rxjs-6-what-changed
-
@LijinDurairaj 因为从 rxjs 5.x 开始进行了重大更改。可能是语法发生了变化。
标签: angular rxjs5 rxjs-pipeable-operators