Angular 无法使用矩调用其类型缺少调用签名的表达式
我正在尝试让Angular 无法使用矩调用其类型缺少调用签名的表达式,angular,typescript,momentjs,Angular,Typescript,Momentjs,我正在尝试让时刻在我的角度应用程序中工作。这里有一个干净的示例来演示这个问题。克隆该repo并在根目录中运行ng build test library。首先安装软件包,npm安装 当我尝试使用矩时,出现以下错误。我做错了什么?我已经试着解决这个问题有一段时间了。我已经在谷歌上搜索了很多次,并尝试了一些建议,但都无济于事 用法: import * as moment from 'moment'; ... public doSomething(): string { const aM
时刻
在我的角度
应用程序中工作。这里有一个干净的示例来演示这个问题。克隆该repo并在根目录中运行ng build test library
。首先安装软件包,npm安装
当我尝试使用矩
时,出现以下错误。我做错了什么?我已经试着解决这个问题有一段时间了。我已经在谷歌上搜索了很多次,并尝试了一些建议,但都无济于事
用法:
import * as moment from 'moment';
...
public doSomething(): string {
const aMoment: moment.Moment = moment(); // line 22
return aMoment.format();
}
错误:
projects/test-library/src/lib/test-library.component.ts(22,36): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'typeof moment' has no compatible call signatures.
tsconfig.json
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"outDir": "../../out-tsc/lib",
"target": "es2015",
"module": "es2015",
"moduleResolution": "node",
"declaration": true,
"sourceMap": true,
"inlineSources": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"types": [],
"lib": [
"dom",
"es2018"
]
},
"angularCompilerOptions": {
"annotateForClosureCompiler": true,
"skipTemplateCodegen": true,
"strictMetadataEmit": true,
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true,
"enableResourceInlining": true
},
"exclude": [
"src/test.ts",
"**/*.spec.ts"
]
}
当Typescript编译器找不到您正在使用的方法的类型定义时,就会发生这种情况。 您需要作为依赖项安装@types/moment
npm i -D @types/moment
在您的例子中,您要导入的是库作为名为
moment
的对象导出的所有内容。这个对象显然没有编译器指出的调用签名
根据图书馆随附的资料,
默认导出是您希望使用的矩函数
因此,将导入更改为以下内容应该可以:
import moment from 'moment';
我也有同样的问题。我用两种方法解决了这个问题 1) 我在compilerOptions下设置了esModuleInteropfalse的值
“esModuleInterop”:在我的情况下为false
esModuleInterop:true
tsconfig.json中有
删除此行后,问题得到解决。您是否尝试过从“时刻”导入类似于导入时刻的内容代码>?在我的案例中,根据这一点是不必要的,因为“时刻”提供了自己的类型。很高兴知道这一点,但错误是特定于类型定义的@ArikaelI从这个导入开始,但当我在本地运行它时,它不起作用,所以我将它从“时刻”
更改为“作为时刻导入”,正如文档所指定的那样。这在本地工作,但在构建服务器上给了我同样的错误。为什么矩在本地的行为与在构建服务器上不同?非常感谢!这次修理救了我。埃斯莫杜利普是个恶棍。