【发布时间】:2019-05-29 20:42:46
【问题描述】:
尝试在 Angular 7 库中使用 moment js 时出错(在普通的 Angular 应用程序中也是如此)
复制步骤:
创建角度库
ng generate library my-lib
npm install moment --save
在组件中尝试使用以下代码
import * as moment from 'moment';
export interface CalendarDate {
mDate: moment.Moment;
}
export class CalenderComponent implements OnInit {
currentDate = moment();
...
当我们尝试ng build my-lib --watch 时会失败并出现以下错误
Building Angular Package
Building entry point 'student'
Compiling TypeScript sources through ngc
Bundling to FESM2015
BUILD ERROR
Cannot call a namespace ('moment')
Error: Cannot call a namespace ('moment')
at error (D:\Eschool\homework\student\node_modules\rollup\dist\rollup.js:346
0:30)
at Module.error (D:\Eschool\homework\student\node_modules\rollup\dist\rollup
.js:13349:9)
at CallExpression.bind (D:\Eschool\homework\student\node_modules\rollup\dist
\rollup.js:10228:30)
at AssignmentExpression.NodeBase.bind (D:\Eschool\homework\student\node_modu
les\rollup\dist\rollup.js:9183:23)
at AssignmentExpression.bind (D:\Eschool\homework\student\node_modules\rollu
p\dist\rollup.js:9919:31)
at ExpressionStatement$$1.NodeBase.bind (D:\Eschool\homework\student\node_mo
dules\rollup\dist\rollup.js:9183:23)
at BlockStatement$$1.NodeBase.bind (D:\Eschool\homework\student\node_modules
\rollup\dist\rollup.js:9179:31)
at FunctionExpression.NodeBase.bind (D:\Eschool\homework\student\node_module
s\rollup\dist\rollup.js:9183:23)
at MethodDefinition.NodeBase.bind (D:\Eschool\homework\student\node_modules\
rollup\dist\rollup.js:9183:23)
at ClassBody.NodeBase.bind (D:\Eschool\homework\student\node_modules\rollup\
dist\rollup.js:9179:31)
at ClassDeclaration$$1.NodeBase.bind (D:\Eschool\homework\student\node_modul
es\rollup\dist\rollup.js:9183:23)
at ExportNamedDeclaration.bind (D:\Eschool\homework\student\node_modules\rol
lup\dist\rollup.js:10641:30)
at Program.NodeBase.bind (D:\Eschool\homework\student\node_modules\rollup\di
st\rollup.js:9179:31)
at Module.bindReferences (D:\Eschool\homework\student\node_modules\rollup\di
st\rollup.js:13303:18)
at Graph.link (D:\Eschool\homework\student\node_modules\rollup\dist\rollup.j
s:21489:20)
at D:\Eschool\homework\student\node_modules\rollup\dist\rollup.js:21584:19
Compilation failed. Watching for file changes...
【问题讨论】:
-
你能在 node_module 中找到文件夹吗?
-
stackoverflow.com/a/51643852/5519328 注释类型使用 moment_.Moment
标签: angular momentjs angular7 angular-library