【发布时间】:2018-02-28 16:11:36
【问题描述】:
在 Angular 5 应用程序(无 API)中传递数字的正确 RXJS 方法是什么?
我已经成功通过了一个带有 Subject 的布尔值:
服务:
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 信息源一无所获。
【问题讨论】:
-
你具体想做什么?
-
将一个数字从一个组件传递到另一个组件,所有这些都位于文件树的末尾。我正在使用@Input 链。我想切换到可观察对象和服务。
-
有什么问题?
-
为什么不能
insertData(number)? -
您能否发布更多您的代码(作为代码,而不是屏幕截图)?
标签: angular rxjs observable behaviorsubject subject-observer