我正在运行一个调用服务类中方法的函数。但是我收到这个错误:ERROR TypeError: this.service.执行不是函数
。
我阅读了Angular 6:ERROR TypeError:“…不是函数”-但它是,但我没有看到我指定的类型有任何问题,我想知道是否还有其他问题。
以下是我的代码摘录:exec.组件. ts
export class ExecComponent {
name: string = "";
age: string;
date: string;
constructor(protected service: ExecutorService){}
executeTask(): void {
this.service.execute(this.name, this.date, this.age).subscribe((result: Result) => {
if (result.isSuccess())
console.log("Success!");
else {
console.log("Error");
}
});
}
exec. service.ts
export class ExecutorService {
execute(name: string, date: string, age: string): Observable<Result>{
return new Observable<Result>();
}
}
感谢帮助,谢谢!
我自己也不太确定为什么,但是经过多次试验,我意识到我的服务代码静态ngInjecttableDef=undesign;
中缺少的这一行导致了错误。由于专有原因,我无法发布服务代码,但希望这能解决某人的问题。
尝试这样更改您的代码我认为您的代码中存在一些错别字
executeTask(): void {
this.service.execute(this.name, this.date, this.age).subscribe((result: Result) => {
if (result.isSuccess()){
console.log("Success!");
}
else {
console.log("Error");
});
这是绑定问题,我将函数传递给事件发射器,并且“this”绑定到子组件。
只是改变了我的功能,因为箭头功能修复了一切。
之前:
this.someService.getReplyFile(file)
.pipe(untilDestroyed(this))
.subscribe(a => { });
}
之后:
downloadReplyFile = (file: UploadedDocument) => {
this.someService.getReplyFile(file)
.pipe(untilDestroyed(this)))
.subscribe(a => { });
}