按照以下步骤安装和使用moment:
npm install moment --save
在 app.component.ts 文件中:
import * as moment from 'moment';
export class AppComponent{
currentDate = moment();
还根据 this article 在 angular.json 中添加了以下代码:
"scripts": ["./node_modules/moment/min/moment.min.js"]
我得到:
TypeError: moment is not a function
请检查重现demo
如果您知道我为什么会收到此错误,请提供帮助。
最佳答案
导入没问题:
import * as moment from 'moment';
您还需要安装类型定义,以便 typescript 编译器可以解析特定于 moment.js 的类型:
npm install @types/moment
关于angular - 在 Angular 7 中使用 moment 时出现错误 :TypeError: moment is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53755490/