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


当前回答

Moment是第三方全球资源。moment对象位于浏览器的窗口上。因此,在你的angular2应用中导入它是不正确的。相反,在加载moment.js文件的html中包含<script>标记。

为了让TypeScript满意,你可以添加

declare var moment: any;

在你的文件顶部,你可以使用它来停止编译错误,或者你可以使用

///<reference path="./path/to/moment.d.ts" />

或者使用tsd来安装moment.d.ts文件,TypeScript可以自己找到这个文件。

例子

import {Component} from 'angular2/core';
declare var moment: any;

@Component({
    selector: 'example',
    template: '<h1>Today is {{today}}</h1>'
})
export class ExampleComponent{
    today: string = moment().format('D MMM YYYY');
}

只需确保在html中添加script标签,否则moment将不存在。

<script src="node_modules/moment/moment.js" />

模块加载力矩

首先,你需要设置一个模块加载器,比如System.js来加载moment commonjs文件

System.config({
    ...
    packages: {
        moment: {
            map: 'node_modules/moment/moment.js',
            type: 'cjs',
            defaultExtension: 'js'
        }
    }
});

然后将moment导入到需要使用的文件中

import * as moment from 'moment';

or

import moment = require('moment');

编辑:

还有一些捆绑器的选项,如Webpack或SystemJS builder或Browserify,将保持窗口对象的时刻。欲了解更多相关信息,请访问它们各自的网站获取指导。

其他回答

对于angar2 RC5,这为我工作…

第一次安装时刻通过NPM

npm install moment --save

然后在你想要使用它的组件中导入moment

import * as moment from 'moment';

最后,在systemjs.config.js中配置"map"和"packages"

// map tells the System loader where to look for things
  var map = {
  ....
  'moment':                     'node_modules/moment'
  };
  // packages tells the System loader how to load when no filename and/or no extension
  var packages = {
    ...
    'moment':                       { main:'moment', defaultExtension: 'js'}
  };

——2017年11月1日更新

Typings现在已弃用,被npm @types所取代。从现在开始,除了npm,获取类型声明将不需要任何工具。要使用Moment,您不需要通过@types安装类型定义,因为Moment已经提供了自己的类型定义。

因此,它简化为3个步骤:

1 -安装时刻,包括类型定义:

npm install moment --save

2 -在HTML文件中添加script标签:

<script src="node_modules/moment/moment.js" />

3 -现在你可以使用它了。时期。

today: string = moment().format('D MMM YYYY');

——原创答案

这只需要3个步骤:

1 -安装力矩定义- *.d。ts文件:

typings install --save --global dt~moment dt~moment-node

2 -在HTML文件中添加script标签:

<script src="node_modules/moment/moment.js" />

3 -现在你可以使用它了。时期。

today: string = moment().format('D MMM YYYY');

2017年4月更新:

从版本2.13.0开始,Moment包含了一个typescript定义文件。https://momentjs.com/docs/#/use-it/typescript/

只要用npm在你的控制台类型中安装它

npm install --save moment

然后在你的Angular应用中,导入就像这样简单:

import * as moment from 'moment';

就是这样,你得到了完全的Typescript支持!

额外编辑:要在Typescript中输入一个变量或属性Moment,你可以这样做,例如:

let myMoment: moment.Moment = moment("someDate");

下面的方法对我很有效。

首先,安装moment的类型定义。

类型安装时刻—保存

(注:NOT -ambient)

然后,为了解决缺乏适当的导出:

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'.