简体   繁体   English

angular 7 Observable、Subject、Subscribe 不立即同步?

[英]angular 7 Observable, Subject, Subscribe not syncing immediately?

Thank you for the help first.首先感谢您的帮助。 Using Angular 7. What I'm trying to do is controlling one variable from several components,使用 Angular 7. 我要做的是从多个组件中控制一个变量,

Made service.ts制作 service.ts

import { Injectable } from '@angular/core'
import { Observable } from 'rxjs'
import { Subject } from 'rxjs/Subject'

@Injectable()
export class GeneralProfileControllerService {
  private subject = new Subject<boolean>()

  clickGeneralProfileController(value: boolean) { 
    this.subject.next(!value) 
  } 

  openGeneralProfileController() {
    this.subject.next(true)
  }

  closeGeneralProfileController() {
    this.subject.next(false)
  }

  getGeneralProfileController(): Observable<any> {
    return this.subject.asObservable()
  }
}

And imported service.ts to component.ts I made when there is a click event, it calls onChangeMode function.并在有点击事件时将service.ts导入到我制作的component.ts中,它调用onChangeMode function。 the problem is when there is no [ this.gpc.clickGeneralProfileController(this.personalInfoEditMode) ] line, personalInfoEditMode value never changes.问题是当没有 [ this.gpc.clickGeneralProfileController(this.personalInfoEditMode) ] 行时,personalInfoEditMode 值永远不会改变。 And if there is the line [ this.gpc.clickGeneralProfileController(this.personalInfoEditMode) ] personalInfoEditMode value changes after 2 clicks.如果有行 [ this.gpc.clickGeneralProfileController(this.personalInfoEditMode) ]personalInfoEditMode 值在 2 次点击后发生变化。 Not one click.不是一键点击。

I don't understand why it is not working.我不明白为什么它不起作用。 Plz, help.请帮忙。 and let me know if there is more code needed for this question.让我知道这个问题是否需要更多代码。 Thanks..谢谢..

  public onChangeMode(): void {
    console.log('node...')
    
    this.gpc.clickGeneralProfileController(this.personalInfoEditMode)
    
    this.subscription = this.gpc
      .getGeneralProfileController()
      .subscribe((value) => { 
        this.personalInfoEditMode = value 
        if (this.personalInfoEditMode) {
          this.initpersonalInfoForm()
        }
      })
     
  }

The reason why you dont see any of changes immediate is pretty common problem.您没有立即看到任何更改的原因是很常见的问题。

The reason is here原因在这里

public onChangeMode(): void {
    console.log('node...')
    
    this.gpc.clickGeneralProfileController(this.personalInfoEditMode)
    
    this.subscription = this.gpc
     .getGeneralProfileController()
     .subscribe((value) => { 
      this.personalInfoEditMode = value 
      if (this.personalInfoEditMode) {
         this.initpersonalInfoForm()
      }
   })
}

In this method you call the subscribe method of a simple subject.在此方法中,您调用简单主题的 subscribe 方法。 A subject only emits the new value to the currently subscribed elements.主题仅向当前订阅的元素发出新值。 So when you call the clickGeneralProfileController() method it emits the value right away but since at that time of the emit there are no subscribers, you will not get anything for the first click.因此,当您调用 clickGeneralProfileController() 方法时,它会立即发出值,但由于在发出时没有订阅者,因此第一次单击将不会得到任何东西。

Also since you call the subscribe method every time when the onChangeMode is called there will be several subscribers with the same handler to the subject so your logic will execute several time.此外,由于每次调用 onChangeMode 时都会调用 subscribe 方法,因此会有多个订阅者对主题具有相同的处理程序,因此您的逻辑将执行多次。 This causes inconsistent behaviour in your code and also causing memory leak.这会导致代码中的行为不一致,还会导致 memory 泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM