小编Noa*_*nak的帖子

Angular RxJs BehaviorSubject 未正确更新

我正在尝试从同级组件(​​灯开关到导航栏)共享一个字符串值。我的问题是财产themeColor当我从 DataService 发出新值时没有得到更新。

这是我的 App.Component.html 的结构:

<navbar></navbar>
<banner><light-switch></light-switch></banner>
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用 DataService:

import {Injectable} from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';

@Injectable()
export class DataService {

  private themeColor = new BehaviorSubject<string>("#f5f0f0");
  currentThemeColor = this.themeColor.asObservable();

  constructor() { }

  changeThemeColor(color: string) {
    this.themeColor.next(color)
  }

}
Run Code Online (Sandbox Code Playgroud)

这是我的 light-switch.component.ts:

import { Component, OnInit } from '@angular/core';
import {DataService} from "./../Services/DataService";

@Component({
  selector: 'light-switch',
  templateUrl: './light-switch.component.html',
  styleUrls: ['./light-switch.component.scss']
})
export class LightSwitchComponent implements OnInit {
  public currentTheme;
  public themeColor;

  constructor(private sanitization: DomSanitizer, private dataService: …
Run Code Online (Sandbox Code Playgroud)

rxjs typescript angular

3
推荐指数
1
解决办法
6018
查看次数

标签 统计

angular ×1

rxjs ×1

typescript ×1