javascript - 为什么我的行为主题订阅只触发一次?
问题描述
在一个 Ionic - angular 项目中,我有一个服务,它在一个名为 FrequencyAnalyzer 的服务中公开启动一个行为主题。
//Somewhere In models.ts
export interface tuneInfo{
note:string,
db: number,
detectedFrequency:number,
detectedKey: number,
key? : Key
}
public processedTune = new BehaviorSubject<tuneInfo | undefined>(undefined);
在这个服务的某个地方。我使用“next”函数来更新行为主体的值。
this.processedTune.next({
note: this.note,
db: maxValueInBuffer,
detectedFrequency: ac,
detectedKey : this.getKeyNumber(ac),
key: this.key
});
然后。在我的一页中。我订阅了processedTune
this.frequencyAnalyzer.processedTune.subscribe(
processedTune => {
//console.log(`db: ${processedTune && processedTune.db}`);
this.currentInfo = {
note: processedTune && processedTune.note || '-',
db: processedTune && processedTune.db || 0,
detectedFrequency: processedTune && processedTune.detectedFrequency || 0,
detectedKey: processedTune && processedTune.detectedKey || 0
};
}
);
this.currentInfo 然后传递给子组件
ngOnChanges(changes: SimpleChanges) {
//things are done with the changes and everything is done properly in this part of the key
}
到目前为止,一切都运行良好。出色的。所以现在我创建了一个新服务,在该服务中我订阅了频率分析器的处理后的行为主题,就像我从页面连接一样
public startSubscription(){
this.frequencyAnalyzer.processedTune.subscribe(
(processedTune => {
console.log("Tune received: ", processedTune);
//console.log(`db: ${processedTune && processedTune.db}`);
this.currentInfo = {
note: processedTune && processedTune.note || '-',
db: processedTune && processedTune.db || 0,
detectedFrequency: processedTune && processedTune.detectedFrequency || 0,
detectedKey: processedTune && processedTune.detectedKey || 0,
key: processedTune && processedTune.key
};
//do more stuff.
}).bind(this));
}
然而。无论行为主体发生多少变化,以及第一次订阅正在检测,该服务都只检测第一个初始状态(未定义)。尽管其他订阅工作正常,但它不会因任何其他更改而触发。
我已经尝试从服务构造函数,从页面调用 startSubscription() ......我还尝试将 undefined 设置为以下形式的一些静态变量: public processesTune = new BehaviorSubject<tuneInfo | 未定义>({dbDetected:0,.....});(遵循接口规范)但似乎没有任何效果。
我究竟做错了什么?第一个订阅对第二个订阅有影响吗?我知道 angular 是懒惰的,但我认为 behaviorsubjects 能够多播到不同的订阅。我觉得有些东西我不完全理解。
更多高级信息。在我的 APP MODULE TS
import { ALL_PROVIDERS } from './providers';
然后providers/index.ts如下
import { BluetoothService } from './bluetooth/bluetooth.service';
import { FrequencyAnalyzerService } from './frequency-analyzer/frequency-analyzer.service';
import { KeyboardService } from './keyboard/keyboard.service';
import { TunerControllerService } from './tuner-controller/tuner-controller.service';
import { SettingsService} from './settings/settings.service';
export * from './bluetooth/bluetooth.service';
export * from './frequency-analyzer/frequency-analyzer.service';
export * from './keyboard/keyboard.service';
export * from './tuner-controller/tuner-controller.service';
export * from './settings/settings.service';
export const ALL_PROVIDERS = [
BluetoothService,
FrequencyAnalyzerService,
KeyboardService,
TunerControllerService,
SettingsService
];
我刚刚意识到每个提供程序中都没有 module.ts。而且我经常重用提供者。提供者也需要 module.ts 吗?
解决方案
推荐阅读
- mysql - 包装在事务中时,Mysql DDL 查询卡在 Waiting for table metadata lock
- python - 如何在python中将数据框名称作为字符串获取
- php - 致命错误:找不到类“主题”。我是不是叫错了班级?
- javascript - 在 javascript 中设置 UTC -5 偏移量
- ios - 如何从一个 VC 中包含的一个 VC 过渡到一个新的 VC,该 VC 将包含在与另一个 VC 相同的 VC 中?
- c++ - 自然数除数的颜色
- vba - 在access数据库中的所有表中添加计算列
- python - 如何组合字典的不同键的每个值以在 Python 中生成数据框?
- html - 如何对齐css网格单元格中的元素?
- asp.net - 如何在 ASP.Net 中使用 Google AMP