【发布时间】:2019-12-12 14:11:38
【问题描述】:
我有一个组件,它有一个方法interval(),当一个按钮被点击时。
interval() {
this.statusInterval = interval(2000).subscribe(x => {
this.interval2 = x;
console.log(this.interval2);
this.assetItemActionService.sendNumber(x);
})
}
在构造函数中,我有这个订阅,它应该更新那个值。
interval2 = 0;
percentSubscription= Subscription;
constructor(private service:Service) {
console.log('takeee');
this.percentSubscription = this.service.number$.subscribe(percent => {
console.log("set number");
this.interval2 = percent;
}, error => console.log(error))
}
最多 2 个代码片段在同一个组件中。
在服务中,我有这个不断更新的主题。
private number: Subject<number> = new Subject();
number$ = this.number.asObservable();
sendNumber(number) {
console.log('received');
this.number.next(number);
}
问题是当我路由到另一个页面时,UI 没有更新,构造函数的部分不再工作,我无法订阅,但在控制台中,值每次都会更新。所以 UI 没有呈现,并且在我回到组件后我不能再订阅那个主题了。
我该如何解决这个问题?
【问题讨论】:
-
您能在 stackblitz 上创建一个最小的复制品吗? stackblitz.com/fork/angular
-
是的,请 5 分钟
-
看不到,正常吗?