angular 7 Observable、Subject、Subscribe 没有立即同步?
angular 7 Observable, Subject, Subscribe not syncing immediately?
首先感谢您的帮助。
使用 Angular 7.
我想做的是从几个组件控制一个变量,
取得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()
}
}
并将 service.ts 导入到 component.ts
我做的是当有点击事件的时候,它调用了onChangeMode函数。
问题是当没有 [ this.gpc.clickGeneralProfileController(this.personalInfoEditMode) ] 行时,personalInfoEditMode 值永远不会改变。
如果有行 [ this.gpc.clickGeneralProfileController(this.personalInfoEditMode) ]
personalInfoEditMode 值在点击 2 次后发生变化。一次都没有。
我不明白为什么它不起作用。请帮助。让我知道这个问题是否需要更多代码。谢谢!
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()
}
})
}
您没有立即看到任何更改的原因是很常见的问题。
原因在这里
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()
}
})
}
在这个方法中你调用了一个简单主题的订阅方法。主题仅向当前订阅的元素发出新值。因此,当您调用 clickGeneralProfileController() 方法时,它会立即发出值,但由于在发出时没有订阅者,您将不会在第一次点击时得到任何东西。
此外,由于您每次调用 onChangeMode 时都会调用 subscribe 方法,因此会有多个订阅者对主题具有相同的处理程序,因此您的逻辑将执行多次。这会导致您的代码出现不一致的行为,还会导致内存泄漏。
首先感谢您的帮助。 使用 Angular 7. 我想做的是从几个组件控制一个变量,
取得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()
}
}
并将 service.ts 导入到 component.ts 我做的是当有点击事件的时候,它调用了onChangeMode函数。 问题是当没有 [ this.gpc.clickGeneralProfileController(this.personalInfoEditMode) ] 行时,personalInfoEditMode 值永远不会改变。 如果有行 [ this.gpc.clickGeneralProfileController(this.personalInfoEditMode) ] personalInfoEditMode 值在点击 2 次后发生变化。一次都没有。
我不明白为什么它不起作用。请帮助。让我知道这个问题是否需要更多代码。谢谢!
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()
}
})
}
您没有立即看到任何更改的原因是很常见的问题。
原因在这里
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()
}
})
}
在这个方法中你调用了一个简单主题的订阅方法。主题仅向当前订阅的元素发出新值。因此,当您调用 clickGeneralProfileController() 方法时,它会立即发出值,但由于在发出时没有订阅者,您将不会在第一次点击时得到任何东西。
此外,由于您每次调用 onChangeMode 时都会调用 subscribe 方法,因此会有多个订阅者对主题具有相同的处理程序,因此您的逻辑将执行多次。这会导致您的代码出现不一致的行为,还会导致内存泄漏。