当使用Angular和Typescript时,如果在.subscribe中的Boolean变量没有更新,可能会有以下几种解决方法:
myBooleanVariable: boolean = false;
myObservable.subscribe(() => {
this.myBooleanVariable = true;
});
myBooleanVariable: boolean = false;
let externalVariable = this.myBooleanVariable;
myObservable.subscribe(() => {
externalVariable = true;
});
这样,即使在.subscribe中无法直接访问到Boolean变量,也可以通过更新外部变量来实现更新。
import { Subject } from 'rxjs';
myBooleanSubject: Subject = new Subject();
myObservable.subscribe(() => {
this.myBooleanSubject.next(true);
});
// 在需要使用Boolean变量的地方订阅myBooleanSubject
this.myBooleanSubject.subscribe(value => {
this.myBooleanVariable = value;
});
使用Subject或BehaviorSubject可以将Boolean变量作为可观察对象进行订阅和更新。
希望这些解决方法对于解决Boolean变量在.subscribe上没有更新的问题有所帮助。