对Angular 2的AoT编译器使用汇总并导入Moment.js [英] Using Rollup for Angular 2's AoT compiler and importing Moment.js
问题描述
我正在尝试针对Angular 2遵循 AoT官方指南,我在我的应用程序中使用Moment.js. Moment.js在我的 packages.json 文件中,并且我使用的是2.15.0版.到目前为止,我一直是这样导入的:
I'm trying to follow the official AoT guide for Angular 2, and I'm using Moment.js in my application. Moment.js is on my packages.json file, and I'm using version 2.15.0. I've been importing it like this so far:
import * as moment from 'moment';
但是当我到达必须运行汇总的那一部分时,我会遇到以下错误:
But when I get to the part where I have to run rollup, I end up with the following error:
无法调用名称空间(片刻")
Cannot call a namespace ('moment')
根据此,这似乎与我导入时刻的方式有关.那么,我该怎么做呢? 我似乎无法以其他任何方式导入时刻.如果我使用
Which appears to be related to the way I import moment according to this. So, how am I supposed to do this? I can't seem to import moment any other way. If I use
import moment from 'moment'
我收到编译错误
外部模块时刻"没有默认导出
External module ''moment'' has no default export
推荐答案
我终于设法摆脱了这两个错误.确实要避免:
I have finally managed to get rid of both errors. Indeed to avoid:
无法调用名称空间(片刻")
Cannot call a namespace ('moment')
您需要使用:
import moment from 'moment'
然后避免
时刻"没有默认导出
"moment" has no default export
您必须添加到tsconfig.json(compilerOptions)中:
You have to add into your tsconfig.json (compilerOptions):
"allowSyntheticDefaultImports": true
编辑17/11/2016
我还必须将以下内容添加到我的rollup-config.js文件中:
I also had to add the following to my rollup-config.js file:
plugins: [
nodeResolve({jsnext: true, module: true}),
commonjs({
include: [
'node_modules/rxjs/**',
'node_modules/moment/**'
]
}
}),
uglify()
]
这篇关于对Angular 2的AoT编译器使用汇总并导入Moment.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!