pageChanged$
通知页面何时更改。我目前的尝试
let activeSubscriptions = 0;
let originalUnsubscribe = Subscription.prototype.unsubscribe;
Subscription.prototype.unsubscribe = function (...args) {
activeSubscriptions--;
return originalUnsubscribe.call(this, ...args);
}
let originalSubscribe = Observable.prototype.subscribe;
Observable.prototype.subscribe = function (...args) {
activeSubscriptions++;
return originalSubscribe.call(this, ...args);
}
let pageChanged$ = new Subject<string>();
let lastActiveSubs = 0;
let lastName = 'Start';
pageChanged$
.subscribe((pageName) => {
console.log(`${lastName} += ${activeSubscriptions - lastActiveSubs}`)
lastActiveSubs = activeSubscriptions
lastName = pageName
})
答案 0 :(得分:0)
我所做的是创建一个订阅数组,其中有一个或多个Observables要监听。 每当我创建订阅时,我都会将其添加到该数组中。然后,当命中ngOnDestroy时,我会浏览所有这些并取消订阅:
@IonicPage()
@Component({
selector: 'my-selector',
templateUrl: 'myselector.html'
})
export class APage {
subscriptions : Array<Subscription> = new Array<Subscription>();
ngOnInit() {
let a_new_subscription = someObservable.subscribe(someFunction);
this.subscriptions.push(a_new_subscription);
//Add as many subscriptions as created
}
//...
ngOnDestroy(){
this.subscriptions.forEach(
subscription => {
subscription.unsubscribe();
}
)
}
}