Javascript SystemJS - 时刻不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35272832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SystemJS - moment is not a function
提问by eestein
I'm using JSPM
, AngularJS
, TypeScript
, SystemJS
and ES6
and my project is running pretty well... unless I try to use momentJS.
我使用JSPM
,AngularJS
,TypeScript
,SystemJS
和ES6
我的项目运行非常好......除非我尝试使用momentJS。
This is the error I get:
这是我得到的错误:
TypeError: moment is not a function
类型错误:moment 不是函数
This is part of the code:
这是代码的一部分:
import * as moment from 'moment';
More:
更多的:
var momentInstance = moment(value);
If I debug it, moment is an object not a function:
如果我调试它,moment 是一个对象而不是一个函数:
This is what my moment.js JSPM package looks like:
这是我的 moment.js JSPM 包的样子:
module.exports = require("npm:[email protected]/moment.js");
I've read a lot and couldn't find a way to solve this... any ideas?
我读了很多书,但找不到解决这个问题的方法……有什么想法吗?
Some things I've read/tried:
我读过/尝试过的一些事情:
How to use momentjs in TypeScript with SystemJS?
如何在带有 SystemJS 的 TypeScript 中使用 momentjs?
https://github.com/angular-ui/ui-calendar/issues/154
https://github.com/angular-ui/ui-calendar/issues/154
https://github.com/jkuri/ng2-datepicker/issues/5
https://github.com/jkuri/ng2-datepicker/issues/5
Typescript module systems on momentJS behaving strangely
momentJS 上的 Typescript 模块系统行为异常
https://github.com/dbushell/Pikaday/issues/153
https://github.com/dbushell/Pikaday/issues/153
Thanks!
谢谢!
回答by Oka
Simply remove the grouping (* as
) from your import statement:
只需* as
从导入语句中删除分组 ( ):
import moment from 'moment';
Without digging too deeply in to the source code, it looks like moment
usually exports a function, that has all kinds of methods and other properties attached to it.
无需深入研究源代码,看起来moment
通常会导出一个函数,该函数具有附加的各种方法和其他属性。
By using * as
, you're effectively grabbing all those properties and attaching them to a newobject, destroying the original function. Instead, you just want the chief export (export default
in ES6, module.exports
object in Node.js).
通过使用* as
,您可以有效地获取所有这些属性并将它们附加到一个新对象上,从而破坏了原始函数。相反,您只需要主要导出(export default
在 ES6 中,module.exports
在 Node.js 中为对象)。
Alternatively, you could do
或者,你可以做
import moment, * as moments from 'moment';
to get the moment function asmoment
, and all the other properties on an object called moments
. This makes a little less sense when converting ES5 exports like this to ES6 style, because moment
will retain the same properties.
获取矩函数为moment
,以及一个名为 的对象上的所有其他属性moments
。这在将这样的 ES5 导出转换为 ES6 样式时意义不大,因为moment
将保留相同的属性。
回答by S. Bleier
This worked for me:
这对我有用:
import moment from 'moment/src/moment'
回答by Patrick
A beginner JS mistake that was giving me the same error:
一个初学者的 JS 错误给了我同样的错误:
foobar(moment) {
console.log(moment().whatever());
}
Naming a parameter moment
breaks access to the moment()
function.
命名参数moment
会中断对moment()
函数的访问。
回答by Honza P.
回答by blazehub
To get moment work as a function, if you are using ES6 and babel, you must import it in this way:
要将 moment 工作作为函数,如果您使用的是 ES6 和 babel,则必须以这种方式导入它:
import moment from 'moment'
and not, as written in the documentation
而不是,如文档中所写
import * as moment from 'moment'