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


当前回答

对于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'}
  };

其他回答

我们现在使用模块,

try import {MomentModule} from 'angular -moment/moment.module';

在NPM安装angar2 -moment后

http://ngmodules.org/modules/angular2-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'.

我认为:

NPM安装时刻——保存

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

“时刻”:“node_modules /时刻”

向包数组添加:

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

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

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

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

下面的方法对我很有效:

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

类型存储库中不存在moment-node。您需要重定向到Definitely Typed,以便使用前缀dt使其工作。

对于ANGULAR CLI用户

使用外部库在这里的文档中:

https://github.com/angular/angular-cli/wiki/stories-third-party-lib

Simply install your library via npm install lib-name --save and import it in your code. If the library does not include typings, you can install them using: npm install lib-name --save npm install @types/lib-name --save-dev Then open src/tsconfig.app.json and add it to the types array: "types":[ "lib-name" ] If the library you added typings for is only to be used on your e2e tests, instead use e2e/tsconfig.e2e.json. The same goes for unit tests and src/tsconfig.spec.json. If the library doesn't have typings available at @types/, you can still use it by manually adding typings for it: First, create a typings.d.ts file in your src/ folder. This file will be automatically included as global type definition. Then, in src/typings.d.ts, add the following code: declare module 'typeless-package'; Finally, in the component or file that uses the library, add the following code: import * as typelessPackage from 'typeless-package'; typelessPackage.method(); Done. Note: you might need or find useful to define more typings for the library that you're trying to use.