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


当前回答

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

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

其他回答

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,将保持窗口对象的时刻。欲了解更多相关信息,请访问它们各自的网站获取指导。

angular2-moment库有像{{myDate | amTimeAgo}}这样的管道用于.html文件中。

这些管道也可以作为组件类(.ts)文件中的Typescript函数来访问。首先按照提示安装库:

npm install --save angular2-moment

在node_modules/angular2-moment现在将是“.pipe.d”。例如calendar.pipe.d。ts, date-format.pipe.d。t等等。

每一个都包含等效管道的Typescript函数名,例如,DateFormatPipe()是amDateFormatPipe的函数。

要在项目中使用DateFormatPipe,请在app.module.ts中将其导入并添加到全局提供程序中:

import { DateFormatPipe } from "angular2-moment";
.....
providers: [{provide: ErrorHandler, useClass: IonicErrorHandler}, ...., DateFormatPipe]

然后在任何你想要使用该函数的组件中,将它导入到顶部,注入到构造函数中并使用:

import { DateFormatPipe } from "angular2-moment";
....
constructor(.......,  public dfp: DateFormatPipe) {
    let raw = new Date(2015, 1, 12);
    this.formattedDate = dfp.transform(raw, 'D MMM YYYY');
}

要使用任何函数都遵循这个过程。如果有一种方法可以访问所有的函数,那就太好了,但上面的解决方案都不适合我。

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

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

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

import * as moment from 'moment'

对于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.

我遵循了允许allowSyntheticDefaultImports的建议(因为没有从时刻到我使用的导出),使用System。然后我听从别人的建议使用:

import moment from 'moment';

在system.js配置中映射时刻。由于某些原因,其他import语句对我不起作用