如何在 angular 2 typescript 应用程序中使用 moment.js 库? [英] How to use moment.js library in angular 2 typescript app?
问题描述
我尝试将它与打字稿绑定一起使用:
I tried to use it with typescript bindings:
npm install moment --save
typings install moment --ambient -- save
test.ts:
import {moment} from 'moment/moment';
没有:
npm install moment --save
test.ts:
var moment = require('moment/moment');
但是当我调用 moment.format() 时,出现错误.应该很简单,有人可以提供可行的命令行/导入组合吗?
But when I call moment.format(), I get an error. Should be simple, can anybody provide a command line/import combination that would work?
推荐答案
2017 年 4 月更新:
从 2.13.0 版本开始,Moment 包含一个打字稿定义文件.https://momentjs.com/docs/#/use-it/typescript/一个>
As of version 2.13.0, Moment includes a typescript definition file. https://momentjs.com/docs/#/use-it/typescript/
只需在您的控制台类型中使用 npm 安装它
Just install it with npm, in your console type
npm install --save moment
然后在您的 Angular 应用程序中,导入就像这样简单:
And then in your Angular app, import is as easy as this:
import * as moment from 'moment';
就是这样,您将获得完整的 Typescript 支持!
That's it, you get full Typescript support!
额外要在 Typescript 中将变量或属性输入为 Moment
,您可以执行以下操作,例如:
Bonus edit: To type a variable or property as Moment
in Typescript you can do this e.g.:
let myMoment: moment.Moment = moment("someDate");
这篇关于如何在 angular 2 typescript 应用程序中使用 moment.js 库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!