Angular 5 BehaviorSubject 不适用于布尔值
Angular 5 BehaviorSubject not working for boolean value
我正在尝试练习 angular 中的 behaviorsubject 5. 我正在编写一个包含两个组件的小应用程序,我想同时更改两个组件中的值,但该值没有改变。 BehaviorSubject 应该更改所有组件中的值。请帮助我理解。
服务
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
@Injectable()
export class TestserviceService {
public isAdmin = new BehaviorSubject<boolean>(false);
cast = this.isAdmin.asObservable();
constructor() { }
changeAdmin(){
this.isAdmin.next(!this.isAdmin);
}
}
组件一
import { Component, OnInit } from '@angular/core';
import{ TestserviceService } from '../../testservice.service';
@Component({
selector: 'app-one',
templateUrl: './one.component.html',
styleUrls: ['./one.component.css']
})
export class OneComponent implements OnInit {
isAdmin: boolean;
constructor(private testservice: TestserviceService) { }
ngOnInit() {
this.testservice.cast.subscribe(data => this.isAdmin = data);
}
changeValue(){
this.testservice.changeAdmin();
console.log(this.isAdmin);
}
}
组件一 html
<button (click)="changeValue()">Click Me</button>
<p>
one {{isAdmin}}
</p>
第二部分
import { Component, OnInit } from '@angular/core';
import { TestserviceService } from '../../testservice.service';
@Component({
selector: 'app-two',
templateUrl: './two.component.html',
styleUrls: ['./two.component.css']
})
export class TwoComponent implements OnInit {
isAdmin: boolean;
constructor(private testservice: TestserviceService) { }
ngOnInit() {
this.testservice.cast.subscribe(data => this.isAdmin = data);
console.log("two "+this.isAdmin);
}
}
将您的服务更改为:
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
@Injectable()
export class SharedServiceService {
constructor() { }
public isAdmin = new BehaviorSubject<boolean>(false);
cast = this.isAdmin.asObservable();
changeAdmin(){
this.isAdmin.next(!this.isAdmin.value);
}
}
应该是this.isAdmin.value
因为this.admin
只会是behaviorSubject的对象
changeAdmin(){
this.isAdmin.next(!this.isAdmin);
}
应该是
changeAdmin(){
this.isAdmin.next(!this.isAdmin.value);
}
this.isAdmin
是一个 BehaviorSubject
并且您试图设置 !thisAdmin
其计算结果为 false
Stackblitz
我正在尝试练习 angular 中的 behaviorsubject 5. 我正在编写一个包含两个组件的小应用程序,我想同时更改两个组件中的值,但该值没有改变。 BehaviorSubject 应该更改所有组件中的值。请帮助我理解。
服务
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
@Injectable()
export class TestserviceService {
public isAdmin = new BehaviorSubject<boolean>(false);
cast = this.isAdmin.asObservable();
constructor() { }
changeAdmin(){
this.isAdmin.next(!this.isAdmin);
}
}
组件一
import { Component, OnInit } from '@angular/core';
import{ TestserviceService } from '../../testservice.service';
@Component({
selector: 'app-one',
templateUrl: './one.component.html',
styleUrls: ['./one.component.css']
})
export class OneComponent implements OnInit {
isAdmin: boolean;
constructor(private testservice: TestserviceService) { }
ngOnInit() {
this.testservice.cast.subscribe(data => this.isAdmin = data);
}
changeValue(){
this.testservice.changeAdmin();
console.log(this.isAdmin);
}
}
组件一 html
<button (click)="changeValue()">Click Me</button>
<p>
one {{isAdmin}}
</p>
第二部分
import { Component, OnInit } from '@angular/core';
import { TestserviceService } from '../../testservice.service';
@Component({
selector: 'app-two',
templateUrl: './two.component.html',
styleUrls: ['./two.component.css']
})
export class TwoComponent implements OnInit {
isAdmin: boolean;
constructor(private testservice: TestserviceService) { }
ngOnInit() {
this.testservice.cast.subscribe(data => this.isAdmin = data);
console.log("two "+this.isAdmin);
}
}
将您的服务更改为:
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
@Injectable()
export class SharedServiceService {
constructor() { }
public isAdmin = new BehaviorSubject<boolean>(false);
cast = this.isAdmin.asObservable();
changeAdmin(){
this.isAdmin.next(!this.isAdmin.value);
}
}
应该是this.isAdmin.value
因为this.admin
只会是behaviorSubject的对象
changeAdmin(){
this.isAdmin.next(!this.isAdmin);
}
应该是
changeAdmin(){
this.isAdmin.next(!this.isAdmin.value);
}
this.isAdmin
是一个 BehaviorSubject
并且您试图设置 !thisAdmin
其计算结果为 false