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


当前回答

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

其他回答

尝试在tsconfig.json中添加" allowsyntheticdefaulultimports ": true。

国旗的作用是什么?

这基本上是告诉TypeScript编译器,可以使用ES6导入语句,即。

import * as moment from 'moment/moment';

在CommonJS模块上,比如Moment.js,它没有声明默认导出。该标志只影响类型检查,不影响生成的代码。

如果你使用SystemJS作为模块加载器,这是必要的。如果你告诉TS编译器你使用SystemJS,该标志将自动打开:

"module": "system"

如果ide被配置为使用tsconfig.json,这也将删除由它们抛出的任何错误。

我自己在Angular中使用Moment的版本

NPM I瞬间——保存

import * as _moment from 'moment';

...

moment: _moment.Moment = _moment();

constructor() { }

ngOnInit() {

    const unix = this.moment.format('X');
    console.log(unix);    

}

首先导入moment作为_moment,然后声明类型为_moment的moment变量。初始值为_moment()的Moment。

简单地导入moment不会给你任何自动补全,但如果你将声明type moment interface from _moment namespace from 'moment' package initialized with moment namespace invoked _moment()会给你自动补全moment的api。

我认为这是使用moment而不使用@types类型或angular提供程序的最具角度的方式,如果你正在寻找像moment这样的普通javascript库的自动完成。

对于Angular 7+(也支持8,9,10,11):

1:通过命令npm Install moment——save安装moment

2:不要在app.module.ts中添加任何东西

3:只需要在你的组件或任何其他文件的顶部添加import语句,就像这样:import * as moment from 'moment';

4:现在你可以在代码的任何地方使用moment。就像:

myDate = moment(someDate)。格式(“MM / DD / YYYY HH: MM”);

——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');

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

NPM安装时刻——保存

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

Import * as moment from "moment";