Javascript 角度:行为主体未按预期工作

Javascript 角度:行为主体未按预期工作,javascript,html,angular,typescript,behaviorsubject,Javascript,Html,Angular,Typescript,Behaviorsubject,我试图得到BevhaviorSubject在角度上的当前值。我将全部内容和值打印到控制台,以使用以下两行检查其内容: console.log(this._isNumeric) console.log(this._isNumeric.getValue()) ……但我得到的是: closed: false hasError: false isStopped: false observers: [] thrownError: null _isScalar: false _value: true va

我试图得到BevhaviorSubject在角度上的当前值。我将全部内容和值打印到控制台,以使用以下两行检查其内容:

console.log(this._isNumeric)
console.log(this._isNumeric.getValue())
……但我得到的是:

closed: false
hasError: false
isStopped: false
observers: []
thrownError: null
_isScalar: false
_value: true
value: true
__proto__: Subject
对于主题(请注意,value参数设置为true)和


如果我只是打印值。也许我犯了一个明显的错误,但是有人知道如何获得行为主体的实际价值吗?使用
.value
而不是
.getValue()
不会改变结果。谢谢!:)

在您的服务中,您可以创建并公开以下行为主体:

private _isNumeric$ = new BehaviorSubject<boolean>(false); // first value is false

/** Protects the _isNumeric$ BehaviorSubject from outside access */
public get IsNumeric$(): Observable<boolean> {
    return this._isNumeric$.asObservable();
}

// NOTE: This is how you can access the value within the service
private get IsNumeric(): boolean {
    return this._isNumeric$.getValue();
}
// In your service file, do this

@Injectable({ provideIn: 'root' })
export class YourServiceName {
  
  // Set the initialvalues in this format ==>
  // initialValuesForBehaviorSubject: YourModel = initialValue;
  // In your case: 
  initialValue: boolean = false;


  // Make a source using Behavior Subject as a type and above initial
  // values as the initial value as a private variable
  // This source is required to SET the data
  // Your case: 
  private behaviorSubjectSource: BehaviorSubject<boolean> = 
       new BehaviorSubject<boolean>(this.initialValue);

  // Create a public observable to GET the data
  // Your case: 
  observableForBehaviorSubject: Observable<boolean> = 
     this.behaviorSubjectSource.asObservable();


  // Create a method to set the data from anywhere in your application
  setData(data: boolean) {
    this.behaviorSubjectSource.next(data);
  }

  // Create a method to get data from anywhere in your application
  getData(): Observable<boolean> {
    return this.observableForBehaviorSubject;
  }
}



///// In your component-a.ts file, set the value like this: 

ngOnInit() {
  this.yourService.setData(true);
}

//// In your component-b.ts, get the value like this: 

ngOnInit() {
  localVariableToStoreObservableData: boolean; 

  this.yourService.getData().subscribe(data => {
     if(data) {
       this.localVariableToStoreObservableData = data;
     }
  })
}
如果您想从组件访问数据,您可以通过这样订阅来检索数据。记住也要取消订阅Behavior主题

this.yourService.IsNumeric$
  .pipe(takeUntil(this.onDestroy$)) // This is just a subject used to unsubscribe later
  .subscribe((value: boolean) => {
    // Use the result from the BehaviorSubject
  });

每个行为主体都需要3样东西:a)初始值,b)可以转化为可观察的源,c)可以订阅的公共变量。您可以像这样使用行为主题:

private _isNumeric$ = new BehaviorSubject<boolean>(false); // first value is false

/** Protects the _isNumeric$ BehaviorSubject from outside access */
public get IsNumeric$(): Observable<boolean> {
    return this._isNumeric$.asObservable();
}

// NOTE: This is how you can access the value within the service
private get IsNumeric(): boolean {
    return this._isNumeric$.getValue();
}
// In your service file, do this

@Injectable({ provideIn: 'root' })
export class YourServiceName {
  
  // Set the initialvalues in this format ==>
  // initialValuesForBehaviorSubject: YourModel = initialValue;
  // In your case: 
  initialValue: boolean = false;


  // Make a source using Behavior Subject as a type and above initial
  // values as the initial value as a private variable
  // This source is required to SET the data
  // Your case: 
  private behaviorSubjectSource: BehaviorSubject<boolean> = 
       new BehaviorSubject<boolean>(this.initialValue);

  // Create a public observable to GET the data
  // Your case: 
  observableForBehaviorSubject: Observable<boolean> = 
     this.behaviorSubjectSource.asObservable();


  // Create a method to set the data from anywhere in your application
  setData(data: boolean) {
    this.behaviorSubjectSource.next(data);
  }

  // Create a method to get data from anywhere in your application
  getData(): Observable<boolean> {
    return this.observableForBehaviorSubject;
  }
}



///// In your component-a.ts file, set the value like this: 

ngOnInit() {
  this.yourService.setData(true);
}

//// In your component-b.ts, get the value like this: 

ngOnInit() {
  localVariableToStoreObservableData: boolean; 

  this.yourService.getData().subscribe(data => {
     if(data) {
       this.localVariableToStoreObservableData = data;
     }
  })
}
//在您的服务文件中,执行以下操作
@可注射({provideIn:'root'})
导出类YourServiceName{
//按此格式设置初始值==>
//initialValuesForBehaviorSubject:YourModel=initialValue;
//就你而言:
初始值:布尔值=false;
//使用行为主题作为类型并在首字母以上创建源
//值作为初始值作为私有变量
//设置数据时需要此源
//你的情况:
private BehaviorSubject来源:BehaviorSubject=
新的BehaviorSubject(this.initialValue);
//创建一个公共可观察对象以获取数据
//你的情况:
ObservieForBehaviorSubject:可观察=
this.behaviorSubjectSource.asObservable();
//创建一个方法,从应用程序中的任何位置设置数据
setData(数据:布尔值){
this.behaviorSubjectSource.next(数据);
}
//创建从应用程序中的任何位置获取数据的方法
getData():可观察{
返回此.observeForBehaviorSubject;
}
}
/////在component-a.ts文件中,设置如下值:
恩戈尼尼特(){
this.yourService.setData(true);
}
////在component-b.ts中,获得如下值:
恩戈尼尼特(){
LocalVariableToStoreObservereData:布尔值;
this.yourService.getData().subscribe(数据=>{
如果(数据){
this.localVariableToStoreObservableData=数据;
}
})
}

有些东西你可以通过反复尝试学习。这不是其中之一。在您喜爱的搜索引擎中查找“行为主题”。TLDR答案是
this.\u isNumeric.subscribe(val=>{/*使用val*/})
您也可以这样做。\u isNumeric.value,它应该为您提供值