Error: Cannot invoke an expression whose type lacks a call signature. (BehaviorSubject)

Error: Cannot invoke an expression whose type lacks a call signature. (BehaviorSubject)

我可以提供一个虚拟应用程序来演示这一点,但它归结为以下内容: 服务文件:dialog.service.ts

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';

@Injectable()
export class DialogService  {
  public TriggerDlgAction: BehaviorSubject<boolean>;

  constructor() {
    this.TriggerDlgAction = new BehaviorSubject<boolean>(false); // initialize
  }
}

app.component.ts

import { Component, OnInit } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { DialogService } from './dialog.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit { 
  triggerValue: boolean = false;

  constructor(private dlgSvc: DialogService ) {

  }
  ngOnInit() {
    this.dlgSvc.TriggerDlgAction.subscribe(
      (doTrigger) => {
        this.triggerValue = doTrigger;
        console.log(this.triggerValue);
      }
    )
  } 
}

和 client.component.ts(其模块导入到 app.module.ts。

import { Component } from '@angular/core';
import { DialogService } from '../dialog.service';

@Component({
  selector: 'app-client',
  templateUrl: './client.component.html',
  styleUrls: ['./client.component.css']
})
export class ClientComponent {

  constructor(protected dlgSvc: DialogService) { }

  RequestAppFunction() {
    this.dlgSvc.TriggerDlgAction<boolean>(true);
  }
}

还有我不明白的错误:

提前致谢:-)

我认为问题出在您尝试仅使用 true 进行调用,因为行为主体不希望这样调用。

需要使用 next() 调用行为主题,并在此处传递值,以便让它们发送给订阅者。

尝试修改以下代码。

import { Component } from '@angular/core';
import { DialogService } from '../dialog.service';

@Component({
  selector: 'app-client',
  templateUrl: './client.component.html',
  styleUrls: ['./client.component.css']
})
export class ClientComponent {

  constructor(protected dlgSvc: DialogService) { }

  RequestAppFunction() {
    this.dlgSvc.TriggerDlgAction.next(true);
                               ^^^^^^^^^
  }
}