我正在查看是否可以combineLatest
在 Angular 服务中使用来删除activeFiler$
switch 块(该服务应该做同样的事情)。这是现在的组件设计(stackblitz 链接),我正在尝试删除除render$
observable 之外的所有属性:
export class TodosComponent implements OnInit {
constructor(private ts:TodoService) {}
render$: Observable<Todo[]>;
activeFilter$: Observable<VISIBILITY_FILTER>;
ngOnInit() {
this.render$ = this.ts.selectedTodos$;
this.activeFilter$ = this.ts.activeFilter$;
this.activeFilter$.subscribe(active=>{
switch (active) {
case VISIBILITY_FILTER.SHOW_COMPLETED:
this.render$ = this.ts.completeTodos$;
break;
case VISIBILITY_FILTER.SHOW_ACTIVE:
this.render$ = this.ts.incompleteTodos$;
break;
default:
this.render$ = this.ts.todos$;
}
});
}
}
}
如图所示,我已初始化为从文件this.render$
返回的 Observable 。todo.service.ts
该方法如下所示:
this.selectedTodos$ =
combineLatest(this.activeFilter$, this.completeTodos$, this.incompleteTodos$, this.todos$, this.applyFilter);
private applyFilter(filter, completeTodos, incompleteTodos, todos): Todo[] {
switch (filter) {
case VISIBILITY_FILTER.SHOW_COMPLETED:
return completeTodos;
case VISIBILITY_FILTER.SHOW_ACTIVE:
return incompleteTodos;
default:
return todos;
}
}
因此,有了所有这些,我认为我应该能够删除this.ts.ostore.observe(ACTIVE_FILTER_KEY).subscribe(active=>{
todos 组件中的块,但是如果我删除了整个应用程序将停止工作。
一件奇怪的事情是,如果我注释掉$activeFilter
订阅,并记录下来:
this.render$ = this.ts.selectedTodos$;
this.render$.subscribe(v=>console.log(v));
当我输入更多待办事项时,它们会被记录下来,但它们不会呈现......有什么想法吗?