【发布时间】:2020-08-11 18:21:36
【问题描述】:
首先感谢您的帮助。 使用 Angular 7。 我要做的是从多个组件中控制一个变量,
Made 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) ] 在点击 2 次后,personalInfoEditMode 值会发生变化。不是一键点击。
我不明白为什么它不起作用。请帮忙。让我知道这个问题是否需要更多代码。谢谢!。
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()
}
})
}
【问题讨论】:
-
我不太确定你想要实现什么,但尝试交换
this.gpc.clickGeneralProfileController(this.personalInfoEditMode)和this.subscription = ...的顺序。您在事件(主题)已经触发后订阅。您必须先订阅。
标签: angular observable angular7 subscribe