在角2打字稿应用程序中使用moment.js [英] Using moment.js in Angular 2 typescript application
问题描述
我挣扎在使用角2打字稿应用程序内moment.js库。即使看完答案后这个问题我无法得到它的工作。
这是我做过什么至今:
- 我安装moment.js使用NPM,这样我就可以找到下node_modules库/力矩/ moment.js
-
我配置System.js检索时刻库:
System.config({
包:{
应用:{
格式为:注册,
defaultExtension:'JS'
},
时刻:{
主:moment.js',
类型:'CJS',
defaultExtension:'JS'
}
},
图:{
片刻:node_modules /时刻
}
}); - 我安装打字稿分型与
分型安装时刻节点--ambient --save
和分型安装时刻--ambient --save
,这样我就可以看到里面分型/主/环境/力矩节点和分型/主/环境/力矩 正确的分型
现在,如果我的code我用进口*从时刻的时刻;
打字稿编译运行平稳,我可以看到的Atom编辑器内正确的建议(如果我开始与时刻()
我可以看到一年()月()等)。但是,如果我跑我的code在浏览器中,它提供了一个错误,说'那一刻不是一个函数(调试,我可以看到的那一瞬间是有很多方法的对象)。
如果我写的从'时刻'进口时刻;
在浏览器中code运行正常,但打字稿编译失败与模块一刻没有默认出口虽然写code,我不能得到任何的Atom建议。
我是什么做错了吗?什么是导入一个角2打字稿应用程序中moment.js(及任何非打字稿库)的正确方法是什么?
进口*从'瞬间'moment_;
const的时刻:moment.MomentStatic =(小于任何>时刻_)['默认'] ||时刻_;
I'm struggling in using moment.js library inside an Angular 2 Typescript app. Even after reading the answer to this question I can't get it to work.
This is what I did so far:
- I installed moment.js using npm, so I can find the library under node_modules/moment/moment.js
I configured System.js to retrieve moment library:
System.config({ packages: { app: { format: 'register', defaultExtension: 'js' }, moment: { main: 'moment.js', type: 'cjs', defaultExtension: 'js' } }, map: { moment: 'node_modules/moment' } });
- I installed typescript typings with
typings install moment-node --ambient --save
andtypings install moment --ambient --save
, so I can see the correct typings inside typings/main/ambient/moment-node and typings/main/ambient/moment
Now, if in my code I use import * as moment from 'moment';
typescript compilation run smooth and I can see the correct suggestion inside Atom editor (if I start with moment().
I can see year(), month(), etc.). However if I run my code inside the browser, it gives an error saying that 'moment is not a function' (debugging I can see that moment is an object with lots of methods).
If I write import moment from 'moment';
the code in the browser runs fine, however typescript compilation fails with 'module moment has no default export' and I can't get any suggestion from Atom while writing code.
What am I doing wrong? What's the correct way to import moment.js (and any non typescript library) inside an Angular 2 typescript application?
import * as moment_ from 'moment';
const moment:moment.MomentStatic = (<any>moment_)['default'] || moment_;
这篇关于在角2打字稿应用程序中使用moment.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!