【问题标题】:Error with moment js in angular 7 library角度 7 库中的时刻 js 出错
【发布时间】: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...

【问题讨论】:

标签: angular momentjs angular7 angular-library


【解决方案1】:

我之前也遇到过同样的问题。要修复它,请在库中使用 moment-es6 而不是 moment。然后就可以了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    • 1970-01-01
    • 2018-11-14
    • 1970-01-01
    相关资源
    最近更新 更多