Angular 角度RXJS可观察到或对象在内部传递数字
RXJS在Angular 5应用程序(无API)中传递数字的正确方法是什么 我已成功通过了一个带有主题的布尔值: 服务:Angular 角度RXJS可观察到或对象在内部传递数字,angular,rxjs,observable,behaviorsubject,subject-observer,Angular,Rxjs,Observable,Behaviorsubject,Subject Observer,RXJS在Angular 5应用程序(无API)中传递数字的正确方法是什么 我已成功通过了一个带有主题的布尔值: 服务: import {Injectable} from '@angular/core'; import {Subject} from 'rxjs/Subject'; @Injectable() export class IsOpened { data = new Subject(); constructor() {} insertData(data){
import {Injectable} from '@angular/core';
import {Subject} from 'rxjs/Subject';
@Injectable()
export class IsOpened {
data = new Subject();
constructor() {}
insertData(data){
this.data.next(data);
}
}
发射器:
toggle(){
this.opening = !this.opening;
this._isOpened.insertData(this.opening);
}
听众:
ngAfterViewInit() {
this._isOpened.data.subscribe((value) => {
if(value) this.opened = true;
else this.opened = false;
}});
}
我在侦听器中有点作弊,因为我不存储收到的值,而是评估它并重新创建布尔值
对我来说很有用,只适合几行
我不能对数字做同样的事情
我如何处理数字?使用数组
Google和许多RXJS信息源都没有产生任何结果。下面是一个如何将Subject/BehaviorSubject与对象一起使用的示例。同样的技术也适用于数字 服务
export class ProductService {
private products: IProduct[];
// Private to encapsulate it and prevent any other code from
// calling .next directly
private selectedProductSource = new BehaviorSubject<IProduct | null>(null);
// Publicly expose the read-only observable portion of the subject
selectedProductChanges$ = this.selectedProductSource.asObservable();
changeSelectedProduct(selectedProduct: IProduct | null): void {
this.selectedProductSource.next(selectedProduct);
}
}
onSelected(product: IProduct): void {
this.productService.changeSelectedProduct(product);
}
ngOnInit() {
this.productService.selectedProductChanges$.subscribe(
selectedProduct => this.product = selectedProduct
);
}
在这种情况下,当用户在一个组件中选择某个组件时,该选择将广播给其他几个组件
读取值的组件
export class ProductService {
private products: IProduct[];
// Private to encapsulate it and prevent any other code from
// calling .next directly
private selectedProductSource = new BehaviorSubject<IProduct | null>(null);
// Publicly expose the read-only observable portion of the subject
selectedProductChanges$ = this.selectedProductSource.asObservable();
changeSelectedProduct(selectedProduct: IProduct | null): void {
this.selectedProductSource.next(selectedProduct);
}
}
onSelected(product: IProduct): void {
this.productService.changeSelectedProduct(product);
}
ngOnInit() {
this.productService.selectedProductChanges$.subscribe(
selectedProduct => this.product = selectedProduct
);
}
在本例中,读取该值的组件将其存储到自己的局部变量中。该变量用于绑定,UI将根据所选产品进行更改
注意:您可以使用没有主题/行为主题的getter/setter实现相同的功能
我这里有一个使用Subject/Behavior Subject的完整示例:
与getter/setter(而不是Subject/BehaviorSubject)的功能完全相同:您具体想做什么?将一个数字从一个组件传递到另一个组件,所有数据都位于文件树的末尾。我使用的是@Input-chains。我想切换到observable和services。有什么问题吗?为什么你不能
插入数据(数字)
?你能发布更多的代码(作为代码,而不是屏幕截图)?我想Angular 5开发人员被鼓励全面切换到observable?在对getter和setter进行内部值交换的情况下,是否有更多的好处?由于我的许多组件都在等待这些值,而且它们更新非常频繁,因此我在重构开始时选择了可观察值。Angular的更改检测已经在模板绑定到结果时为您处理通知。在这种情况下,使用getter/setter更简单、更直接。但是,当您需要通知或想要处理异步操作时,可以使用可观察对象(例如vs promises),因为这不是我的示例,并且与提供的github不匹配。如果您有一个更准确的答案,请随意复制我的代码,将其更改为与您的数字示例相匹配,并将其标记为您的答案。:-)