行为主题自动更新值,无需订阅

时间:2018-11-22 08:05:59

标签: angular

我试图在一个组件中为行为主体设置一个值,我想要 访问另一个组件中的相同值,但是我的值是自动更新值

   public workitemTemp: BehaviorSubject<any> = new BehaviorSubject(null);  

   setWorkitemTemp(value: any) {
      this.workitemTemp.next(value);
   }  

   this._sharingService.getWorkItemTempObj().subscribe(res => {
        if (res != null) {
          this.showEnvelope(res)
        }
   });

1 个答案:

答案 0 :(得分:0)

使用新变量作为Observable而不是返回Observable的方法。然后只需订阅该变量。试试这个:

 public workitemTemp: BehaviorSubject<any> = new BehaviorSubject(null);
 public WorkItemTempObj= this.workitemTemp.asObservable();


  setWorkitemTemp(value: any) {
    this.workitemTemp.next(value);
  }  

  this._sharingService.getWorkItemTempObj.subscribe(res => {
     if (res != null) {
        this.showEnvelope(res)
    }
 });