我们创建了一个运算符函数,用于在组件被销毁时取消订阅任何订阅。我们正在使用 ng-take-un-til-destroy 库来帮助解决这个问题。
在升级到Angular 6/7之前,这工作得很好
import { Subscription } from "rxjs";
import { Observable } from "rxjs/internal/Observable";
import { untilDestroyed } from "ngx-take-until-destroy";
declare module "rxjs/internal/Observable" {
interface Observable<T> {
subscribeUntilDestroy: (target, action?, error?, complete?) => Subscription;
}
}
Observable.prototype.subscribeUntilDestroy = function(target, action?, error?, complete?): Subscription {
return this.pipe(untilDestroyed(target)).subscribe(action, error, complete);
};
它应该像我们使用"subscribe"一样工作,但是浏览器javascript控制台错误地说"subscribeUntilDestroy"不是一个函数。
有什么想法吗?
以下是我们尝试使用它的方式:
let interval = observableInterval(SessionService.MAX_IDLE_TIME).pipe(delay(5000));
interval.subscribeUntilDestroy(
this,
value => this.monitor()
);
这似乎是我们环境的问题
我想通了。可观察量的导入必须只是"rxjs"而不是内部导入。