Dro*_*esz 3 rxjs typescript angular
我试图在我的 angular 项目中提交一个 Observable,但它说:“...... .map 不是一个函数”所以,你能解释一下我的代码有什么问题吗?
tasks.component.ts (地图也从 rxjs/operators 导入)
ngOnInit() {
this.tasks = this.tasksService.tasks.map(x => x.filter(y => y.isDone == this.completed));
this.tasksService.getTasks();
}
Run Code Online (Sandbox Code Playgroud)
这是我提供 Observable 的服务:
任务.service.ts
export class TasksService {
private _tasks = new BehaviorSubject<Task[]>([]);
tasks = this._tasks.asObservable();
constructor(private http : Http) {
console.log('Tasks Service is initialized...');
}
getTasks() {
this.http.get(URL).subscribe(x => {
this._tasks.next(x.json());
});
}
Run Code Online (Sandbox Code Playgroud)
map如果您使用的rxjs版本低于 6,请像这样导入。
import 'rxjs/add/operator/map';
Run Code Online (Sandbox Code Playgroud)
如果您使用的是rxjs版本 6 或更高版本,那么首先您必须像这样导入操作符:
import { map } from 'rxjs/operators';
Run Code Online (Sandbox Code Playgroud)
其次,你必须使用pipebefore map:
this.tasks = this.tasksService.tasks.pipe(
map(x => x.filter(y => y.isDone == this.completed))
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6765 次 |
| 最近记录: |