我试图在BehaviorSubject对象中设置一个值,但我没有得到它,每当我使用next(val)方法通过将值插入到其中时,应用程序屏幕只会变黑,这是我做错了在代码中?
preferencesService.ts
import { Injectable } from "@angular/core";
import { Storage } from "@ionic/storage";
import { BehaviorSubject } from "rxjs";
@Injectable({
providedIn: "root"
})
export class PreferencesService {
private theme: BehaviorSubject<any>;
constructor() {
this.theme = new BehaviorSubject("dark-theme");
}
setActiveTheme(val) {
console.log(this.theme);
this.theme.next(val);
}
getActiveTheme() {
return this.theme.asObservable();
}
}
在 app.component.ts 中,我通过以下方式设置值:
toggleAppTheme() {
if (this.selectedTheme === 'dark-theme') {
this.preferencesService.setActiveTheme('light-theme');
} else {
this.preferencesService.setActiveTheme('dark-theme');
}
}