这不是取消行为主体的唯一方法:
maxSub;
fromSub;
toSub;
pageNumberSub;
originalMaxSub;
selectionSub;
selectionOfInterestSub;
constructor(private store: StoreService) {}
ngAfterContentChecked(){
this.maxSub = this.store.max$.subscribe((max) => {
if(max !== null && max !== undefined){
this.max = max;
this.maxPages = Math.floor(this.max / Variables.to);
this.pages = Array.from(Array(this.maxPages), (x, i) => i + 1);
}
}, (error) => console.log(error), () => {});
this.fromSub = this.store.from$.subscribe((from) => {
if(from !== null && from !== undefined) {this.from = from; this.split = this.to - (this.from - 1)}
}, (error) => console.log(error), () => {});
this.toSub = this.store.to$.subscribe((to) => {
if(to !== null && to !== undefined) {this.to = to; this.split = this.to - (this.from - 1)}
}, (error) => console.log(error), () => {});
this.pageNumberSub = this.store.pageNumber$.subscribe((pageNumber) => {
if(pageNumber !== null && pageNumber !== undefined) {this.page = pageNumber;}
}, (error) => console.log(error), () => {});
this.originalMaxSub = this.store.originalMax$.subscribe((originalMax) => {
if(originalMax !== null && originalMax !== undefined) {this.originalMax = originalMax;}
}, (error) => console.log(error), () => {});
this.selectionSub = this.store.selection$.subscribe((selection) => {
if(selection !== null && selection !== undefined) this.selectedAmount = selection.length;
}, (error) => console.log(error), () => {});
this.selectionOfInterestSub = this.store.selectionOfInterest$.subscribe((selectionOfInterest) => {
if(selectionOfInterest !== null && selectionOfInterest !== undefined) this.selectionOfInterest = selectionOfInterest;
}, (error) => console.log(error), () => {});
}
ngOnDestroy(){
this.maxSub.unsubscribe();
this.fromSub.unsubscribe();
this.toSub.unsubscribe();
this.pageNumberSub.unsubscribe();
this.originalMaxSub.unsubscribe();
this.selectionSub.unsubscribe();
this.selectionOfInterestSub.unsubscribe();
}
在常规的旧Observable中,您可以:
alive = true;
constructor(private store: StoreService) {}
ngAfterContentChecked(){
this.store.thing.subscribe().takeWhile(this.alive)...
}
ngOnDestroy(){
this.alive = false;
}
这要简单得多。
没有办法对行为主体做同样的事情吗?
另一件事:我是否缺少行为主体的基本属性,即无论调用多少次订阅,它们在一个类中仅预订一次,这就是为什么行为主体上没有takeWhile()
这样的事情吗? / p>
答案 0 :(得分:1)
1-实际上,您所说的一种更简单的方法并不会完全取消订阅,而只是停止阅读来自可观察对象的内容。因此,请使用退订或完成。
2-由于您正在同时退订,并且您显然不使用Subject进行其他任何操作,因此可以对所有订阅使用相同的Subject变量,然后可以使用一行代码退订不需要初始化所有这些Subject实例。