Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何在一个主题中只保留一个参数?_Angular_Rxjs_Reactive Programming_Subject Observer - Fatal编程技术网

Angular 如何在一个主题中只保留一个参数?

Angular 如何在一个主题中只保留一个参数?,angular,rxjs,reactive-programming,subject-observer,Angular,Rxjs,Reactive Programming,Subject Observer,我从一个控制器向一个对象输入一个参数 selectImport(event){ this.importacionService.onChildSelectImportChanged.next(event); } 从我的组件importacion.component.ts中,我订阅了这个主题 this.importacionService.onSelectImportChanged.subscribe( addImport => { this.importac

我从一个控制器向一个对象输入一个参数

  selectImport(event){
      this.importacionService.onChildSelectImportChanged.next(event);
  }
从我的组件
importacion.component.ts
中,我订阅了这个主题

this.importacionService.onSelectImportChanged.subscribe( addImport => {

    this.importacionService.addImport(this.month, addImport);
   });


  }
现在一切都很好,但当我关闭importacion.component.ts的对话框并再次执行时,主题会创建第二个suscbripcion,因此会创建两个观察者,然后当我使用.next()输入新数据时,订阅服务器会运行两次

我如何控制这种行为,
我的预期行为是,仅在我的主题中存在和参数,并且仅在其值更改时运行

private subscription: Subscription;

ngOnInit() {
    this.subscription = this.importacionService.onSelectImportChanged.subscribe(...);
}

ngOnDestroy() {
    if (this.subscription){
        this.subscription.unsubscribe();
    }
}

您需要取消订阅您自己创建或管理的观察对象

您只需在关闭对话框时取消订阅我收到一条错误消息“object unsubscribed”我试过这样做,但当我再次打开对话框时,它会生成一个错误。请看:尝试使用输入和输出在组件之间传递数据。如果有人想知道如何获得“object unsubscribed”,我会收到一条错误消息“object unsubscribed”错误:通过对主题而不是从
.subscribe
@luisruizfiguero返回的订阅调用unsubscribe来获得它。更新的代码示例将从订阅而不是主题取消订阅。