如果这是在Stack溢出的某个地方,我很抱歉,但是我找不到一个对我有用的答案。
我有一个BehaviorSubject,它以布尔值的形式跟踪状态。我还有一个公共getter方法,它是一个可观察的返回BehaviorSubject的方法。我这样做不仅可以订阅BehaviorSubject值中的更改,而且还可以在未更改的情况下获得当前值。虽然我能够从订阅中获得适当的值,但在调用getter时,我将得到一个返回的对象。我要它返回最后一个值。
我试过使用.getValue(),它似乎不起作用,我也尝试过.value,它看起来确实有效,但在IDE中抛出了一个错误。
我创建了一个非常基本的示例来说明我要完成的任务,并在my.service.ts文件中添加了注释,以显示我要做的事情。请给我建议。谢谢!
编辑:默认情况下,我使用堆栈闪电战中的内容,并将getter分配给name变量来查看它。我的目标是得到价值,这样我就可以做一些基于价值是对还是错的事情。我肯定我错过了一些很简单的东西。
编辑:根据请求向问题添加代码
编辑:我还添加了.subscribe以表明,虽然我理解可观察性是如何工作的,但如果可能的话,我还想使用相同的getter检查值更改之外的值。
MyService
import { Observable, BehaviorSubject } from "rxjs";
export class MyService {
constructor() {}
private _isBoolean = new BehaviorSubject<boolean>(false);
public get isBoolean(): Observable<boolean> {
//return this._isBoolean.value; // Returns boolean, but throws error "Type 'boolean' is not assignable to type 'Observable<boolean>'."
return this._isBoolean; // Returns object, but I want the value
}
}AppComponent
import { Component } from '@angular/core';
import { MyService } from './my.service';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name;
constructor(private _myservice: MyService) {
this._myservice.isBoolean.subscribe(val => {
console.log("value from subscribe: " + val)
})
this.name = this._myservice.isBoolean;
if (this._myservice.isBoolean) {
console.log("true!");
} else if (!this._myservice.isBoolean) {
console.log("false!");
}
}
}发布于 2020-08-04 20:24:45
感谢拉赫马特·阿里带领我找到了我需要的答案。我在这里提供一个答案,以便更容易地找到,提供一个更简单的解决方案示例,以及一个更好的解释。
我希望有一个getter函数,使我既可以订阅对BehaviorSubject值的更改,也可以根据当前的需要获得不需要订阅的当前值。
它不起作用的原因是,在另一个示例中,我让getter返回一个可观察的类型,而不是BehaviorSubject类型。所以,我在控制台中得到了一个“真”结果,因为我检查的是它的存在性,而不是布尔值。一旦这种情况改变,我就能够使用BehaviorSubject属性访问.value的布尔值。
下面是解决方案的一个简化示例(注意: getter没有必要,因为一旦getter返回BehaviorSubject,我就可以使用.next更改它的值,而不需要创建setter):
import { Component } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular';
private _isBooleanSubejct = new BehaviorSubject<boolean>(false);
public get isBoolean(): BehaviorSubject<boolean> {
return this._isBooleanSubejct;
}
constructor() {
console.log(this.isBoolean.value);
this.isBoolean.subscribe(val => {
console.log(val); // false until the timer completed
console.log(this.isBoolean.value); // false until the timer completed
});
setTimeout(() => {
this.isBoolean.next(true);
}, 5000);
}
}发布于 2020-08-04 17:15:51
您正在错误地使用Observables。请将subscribe调用添加到observable中
this._myservice.isBoolean.subscribe((val) => {
this.name = val;
console.log("value: " + val);
});您只需在subscribe回调中添加代码即可使用更改的值:
app.component.ts 编辑
import { Component } from '@angular/core';
import { MyService } from './my.service';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name;
constructor(private _myservice: MyService) {
this._myservice.isBoolean.subscribe(val => {
console.log("value from subscribe: " + val);
this.name = val ;
if (val) {
console.log("true!");
} else if (!val) {
console.log("false!");
}
})
}
}发布于 2020-08-04 17:31:46
使用布尔BehaviorSubject的正确方法:-
定义如下:-
_isBoolean: BehaviorSubject<boolean> = new BehaviorSubject(false);更新如下:-
this._isBoolean.next(true); // for true
this._isBoolean.next(false); // for false在其他组件中读取值:-
this._isBoolean.subscribe(value => {
console.log(value);
});立即读取当前值一次:-
this._isBoolean.asObservable().pipe(take(1)).subscribe(value => {
console.log(value);
});https://stackoverflow.com/questions/63251576
复制相似问题