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


当前回答

我认为:

NPM安装时刻——保存

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

“时刻”:“node_modules /时刻”

向包数组添加:

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

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

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

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

其他回答

不确定这对人们来说是否仍然是一个问题,然而……使用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";

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

国旗的作用是什么?

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

import * as moment from 'moment/moment';

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

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

"module": "system"

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

Moment.js现在在v2.14.1支持TypeScript。

参见:https://github.com/moment/moment/pull/3280

我认为:

NPM安装时刻——保存

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

“时刻”:“node_modules /时刻”

向包数组添加:

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

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

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

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