Angular:成员<function-name>不可调用

kar*_*una 13 angular

我将导入的函数分配给组件属性constructor,以便在其中使用它template.

构建正确,但在我的编辑器(带有Angular语言服务的Visual Studio代码)中,显示以下错误.

错误

下面是我导出的函数的代码.

export function downloadFile(fileUrl: string, fileName: string) {
    let a = document.createElement('a');
    a.href = fileUrl;
    a.download = fileName;
    a.click();
}
Run Code Online (Sandbox Code Playgroud)

下面是我在组件中的代码.

import { downloadFile } from '../../shared/utilities';

@Component({
    ...
})
export class SomeComponent {
    downloadFile: any;

    constructor() {
        this.downloadFile = downloadFile;
    }
}
Run Code Online (Sandbox Code Playgroud)

我无法理解为什么会出现此错误.请帮忙!

Arj*_*ngh 23

我认为不是在组件文件中以这种方式声明下载文件

downloadFile: any;

你应该这样声明

downloadFile: () => any;

检查一下是否有帮助.


Cri*_*ìna 11

以防万一它会帮助某人,我遇到了同样的问题,上面的答案甚至与解决方案都不接近。经过一番尝试,我发现这是由名称冲突引起的。我有一个与方法同名的模板变量。Angular 试图调用模板变量,它最终显示了该错误消息。例如:

// template
<form #foo (ngSubmit)="foo()">

// typescript
foo() {
...
}
Run Code Online (Sandbox Code Playgroud)

它说:

[Angular] 成员foo不可调用