首页 > 解决方案 > 为什么我的行为主题订阅只触发一次?

问题描述

在一个 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 吗?

在此处输入图像描述

标签: javascriptangularionic-frameworkrxjs

解决方案


推荐阅读