gpt4 book ai didi

javascript - SystemJS - 时刻不是函数

转载 作者:IT王子 更新时间:2023-10-29 03:20:13 28 4
gpt4 key购买 nike

我正在使用 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com