javascript - SystemJS - 时刻不是函数

标签 javascript angularjs momentjs systemjs

我正在使用 JSPMAngularJSTypeScriptSystemJSES6 并且我的项目运行得很好......除非我尝试使用 momentJS。

这是我得到的错误:

TypeError: moment is not a function

这是部分代码:

import * as moment from 'moment';

更多:

var momentInstance = moment(value);

如果我调试它,moment 是一个对象而不是一个函数:

enter image description here

这是我的 moment.js JSPM 包的样子:

module.exports = require("npm:moment@2.11.0/moment.js");

我读了很多书,但找不到解决这个问题的方法……有什么想法吗?

我读过/试过的一些东西:

How to use momentjs in TypeScript with SystemJS?

https://github.com/angular-ui/ui-calendar/issues/154

https://github.com/jkuri/ng2-datepicker/issues/5

Typescript module systems on momentJS behaving strangely

https://github.com/dbushell/Pikaday/issues/153

谢谢!

最佳答案

只需从导入语句中删除分组 (* as):

import moment from 'moment';

无需深入研究 source code ,它看起来像 moment 通常导出一个函数,它有各种方法和附加的其他属性。

通过使用 * as,您可以有效地获取所有这些属性并将它们附加到一个 对象,从而破坏原始函数。相反,您只需要主要导出(ES6 中的 export default,Node.js 中的 module.exports 对象)。

或者,你可以这样做

import moment, * as moments from 'moment';

获取矩函数作为 moment,以及对象上称为moments 的所有其他属性。在将这样的 ES5 导出转换为 ES6 样式时,这没有多大意义,因为 moment 将保留相同的属性。

关于javascript - SystemJS - 时刻不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35272832/

相关文章:

javascript - 如何将 Ajax 调用包含为外部文件?

javascript - 如何在 Ace 编辑器中启用搜索框?

javascript - Moment.js 设置服务器的基准时间

javascript - moment.js startOf 问题

javascript - 在异步函数之外使用 await

angularjs - Spawn 命令在 Electron 应用程序中不起作用

javascript - 为什么 `on` 处理程序调用的顺序不按 Angular 排列?

javascript - AngularJS-ng :model - Field is readonly when bound to $q promise?

momentjs - 使用 Momentjs 比较两个日期

javascript - 如何动态更改 anchor 标记链接?