首页 > 解决方案 > 错误:无法调用其类型缺少调用签名的表达式。(行为主体)

问题描述

我可以提供一个虚拟应用程序来演示这一点,但它归结为以下内容:服务文件: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);
  }
}

我不明白的错误: 在此处输入图像描述

提前致谢, :-)

标签: angularrxjsbehaviorsubjectmethod-signature

解决方案


我认为问题出在你试图用 just 打电话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);
                               ^^^^^^^^^
  }
}

推荐阅读