当我使用BehaviorSubject的next()方法时,离子4屏幕变黑

时间:2019-03-13 19:20:22

标签: angular ionic-framework ionic4

我试图在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();
  }
}

enter image description here

app.component.ts 中,我通过以下方式设置值:

toggleAppTheme() {
if (this.selectedTheme === 'dark-theme') {
  this.preferencesService.setActiveTheme('light-theme');
} else {
  this.preferencesService.setActiveTheme('dark-theme');
}

}

0 个答案:

没有答案