kts*_*gop 6 observable redux angular
我几乎可以肯定我应该,但我没有找到有关Redux文档的任何具体信息.我的大多数Angular组件中的模式是我订阅/取消订阅Redux商店,如:
import { Component, OnInit } from '@angular/core';
@Component({
moduleId: module.id,
selector: 'selector',
templateUrl: 'name.component.html'
})
export class ComponentNameComponent implements OnInit {
private unsubscribe : Function;
constructor(@Inject(AppStore) private store: Store<AppState>) {}
ngOnInit() {
this.unsubscribe = this.store.subscribe ( ()=>{ this.updateFromState(); });
}
// Is unsubscribing onDestroy necessary?
ngOnDestroy(){
this.unsubscribe();
}
this.updateFromState(){
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
所以我想知道我是否应该总是取消订阅商店,如果我没有,会发生什么.
是的,您应该(unsubscribe)在不使用应用程序时销毁所有可观察物。我不知道Redux store,但是我觉得你应该仍然杀死你的观测站onDestroy。
如果不取消订阅(),会发生什么?
第一次加载组件时ngOnInit()将进行订阅,store如果再次返回其他组件然后再次访问同一组件,则将再次subscription订阅,那么您将拥有新组件以及上一个组件(共2个订阅)。subscription当您重新访问该组件时,此计数增加了很多倍,这将降低应用程序的性能。如此安全的一面,您应该先杀死上subscription一个,然后再创建新的(通常在中完成)ngOnDestroy()。
| 归档时间: |
|
| 查看次数: |
1627 次 |
| 最近记录: |