加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

是否有必要取消订阅Angular组件中的Redux存储

发布时间:2020-12-17 07:01:54 所属栏目:安全 来源:网络整理
导读:我几乎可以肯定我应该,但我没有找到有关Redux文档的任何具体信息.我的大多数Angular组件中的模式是我订阅/取消订阅Redux商店,如: import { Component,OnInit } from '@angular/core';@Component({ moduleId: module.id,selector: 'selector',templateUrl: '
我几乎可以肯定我应该,但我没有找到有关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(){
        // ...
    }
}

所以我想知道我是否应该总是取消订阅商店,如果我没有,会发生什么.

解决方法

是的,您应该在不使用时销毁(取消订阅)应用程序中的所有可观察对象.我不知道Redux商店,但我觉得你仍然应该在onDestroy中杀死你的observable.

如果您不取消订阅(),会发生什么?

第一次加载组件时,ngOnInit()将订阅存储,现在再次返回其他组件,然后重新访问相同的组件,您将获得新的订阅以及之前的订阅(因此有2个订阅).重新访问组件时,此订阅计数会增加许多倍,这会降低应用程序的性能.如此安全的一面,您应该在创建新订阅之前杀死之前的订阅,这通常在ngOnDestroy()中完成.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读