angular - 在 Angular 7 中使用 moment 时出现错误 :TypeError: moment is not a function

标签 angular momentjs

按照以下步骤安装和使用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/

相关文章:

javascript - moment.js diff 显示错误结果

javascript - 如何将图像传递给函数 Ionic 2

angular - 为什么在此 ngrx 示例中需要 takeUntil?

html - 从 angular 4 中的 html 中删除主机组件标签

javascript - 来自 next() 的 Later.js 错误响应

javascript - 使用 moment 来操作日期和时间

Angular CORS 请求被阻止

angular - 属性 'performance' 在类型 'FirebaseApp' 中缺失,但在类型 'App' 中是必需的

timezone - 将时刻时区用于 future 事件,我应该保存什么?

momentjs - moment.js : how to initialize from DD/MM/YYYY format