angular - 错误:无法调用其类型缺少调用签名的表达式。(行为主体)
问题描述
我可以提供一个虚拟应用程序来演示这一点,但它归结为以下内容:服务文件: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);
}
}
提前致谢, :-)
解决方案
我认为问题出在你试图用 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);
^^^^^^^^^
}
}
推荐阅读
- vbscript - 如何使用 VBS-SAPI-VOICE 读取所有参数?
- node.js - 每 24 小时删除一次用户数据,node.js postgres
- flutter - 有没有办法按字符串中的数字对字符串列表进行排序?
- ios - 如何在 iOS Swift 中为 CMSampleBufferGetFormatDescription 创建 anAudioSampleBuffer
- r - R:如何判断单词中重复字母出现的位置以添加到数据框中
- c# - MVC 消除了在视图中的模型绑定中预先修复项目名称的需要
- macos - macOS 上使用 mach_absolute_time 的 NASM x64 - 无法获得纳秒(工作代码审查)
- reactjs - 将位置道具传递给未路由的组件
- python - Pytest 测试类值
- php - 我可以选择一个虚拟表,它的数据并将其用于 php 的其他内容吗?