Angular 7库中的矩js错误

时间:2019-05-29 20:42:47

标签: angular momentjs angular7 angular-library

尝试在angular 7库中使用矩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...

1 个答案:

答案 0 :(得分:0)

我以前有过同样的问题。要解决此问题,请在您的库中使用moment-es6而不是moment。这样就可以了。