我尝试使用它与typescript绑定:

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()时,我得到一个错误。 应该是简单的,谁能提供一个命令行/导入组合,将工作?


当前回答

不确定这对人们来说是否仍然是一个问题,然而……使用SystemJS和MomentJS作为库,这为我解决了这个问题

/*
 * Import Custom Components
 */
import * as moment from 'moment/moment'; // please use path to moment.js file, extension is set in system.config

// under systemjs, moment is actually exported as the default export, so we account for that
const momentConstructor: (value?: any) => moment.Moment = (<any>moment).default || moment;

在那里工作对我来说很好。

其他回答

要在Typescript项目中使用它,你需要安装moment:

NPM安装时刻——保存

在安装moment之后,你可以在导入后的任何.ts文件中使用它:

Import * as moment from "moment";

使用ng命令行

> npm install moment --save

在app.module

import * as moment from 'moment';

providers: [{ provide: 'moment', useValue: moment }]

在组件

constructor(@Inject('moment') private moment)

这样你就导入了一次时刻

5 . UPDATE Angular =>

{
   provide: 'moment', useFactory: (): any => moment
}

对我来说,在aot中工作 还有通用的

除了利用时间,我什么都不喜欢。时刻 我得到了

Error   Typescript  Type 'typeof moment' is not assignable to type 'Moment'. Property 'format' is missing in type 'typeof moment'.

如果你愿意添加更多的第三方包,我使用了angular2-moment库。安装非常简单,您应该遵循README上的最新说明。因此,我还安装了类型。

它对我来说就像一个魅力,几乎没有添加任何代码来让它工作。

我认为:

NPM安装时刻——保存

在systemjs.config.js文件的map数组中添加:

“时刻”:“node_modules /时刻”

向包数组添加:

- = ytet -伊甸园字幕组= -翻译:

在你的组件中。ts使用: Import * as moment from 'moment/moment';

就是这样。你可以从组件的类中使用:

今天:string = moment()。format('D MMM YYYY');

在systemjs中,我所做的是。我添加了map

map: {
   .....,
   'moment': 'node_modules/moment/moment.js',
   .....
}

然后你可以很容易地导入力矩

import * as moment from 'moment'