pei*_*ent 71 momentjs typescript jspm
我正在尝试学习Typescript.虽然我认为它不相关,但我正在使用VSCode进行此演示.
我有一个package.json
包含这些内容的部分:
{
"devDependencies": {
"gulp": "^3.9.1",
"jspm": "^0.16.33",
"typescript": "^1.8.10"
},
"jspm": {
"moment": "npm:moment@^2.12.0"
}
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个这样的Typescript类main.js
:
import moment from 'moment';
export class Main {
}
Run Code Online (Sandbox Code Playgroud)
我gulpfile.js
看起来像这样:
var gulp = require('gulp');
var typescript = require('gulp-tsb');
var compilerOptions = {
"rootDir": "src/",
"sourceMap": true,
"target": "es5",
"module": "amd",
"declaration": false,
"noImplicitAny": false,
"noResolve": true,
"removeComments": true,
"noLib": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true
};
var typescriptCompiler = typescript.create(compilerOptions);
gulp.task('build', function() {
return gulp.src('/src')
.pipe(typescriptCompiler())
.pipe(gulp.dest('/dest'));
});
Run Code Online (Sandbox Code Playgroud)
当我运行gulp build时,我收到消息: "../main.ts(1,25): Cannot file module 'moment'."
如果我使用import moment = require('moment');
那么jspm将工作并在我运行应用程序时引入模块,但我仍然收到构建错误.我也尝试过:
npm install typings -g
typings install moment --ambient --save
Run Code Online (Sandbox Code Playgroud)
它没有让问题变得更好,而是变得更糟.现在我在构建时遇到以上错误以及以下错误:"../typings/browser/ambient/moment/index.d.ts(9,21): Cannot find namespace 'moment'."
如果我转到typings提供的文件并添加到文件的底部:
declare module "moment" { export = moment; }
Run Code Online (Sandbox Code Playgroud)
我可以得到第二个错误消失,但我仍然需要require语句来获取在我的main.ts
文件中工作的时刻,我仍然得到第一个构建错误.
我是否需要.d.ts
暂时创建自己的文件,或者只是我缺少一些设置文件?
Aid*_*des 89
更新
显然,时刻现在提供了自己的类型定义(根据sivabudh至少从2.14.1向上),因此你根本不需要typings
或根本不需要@types
.
import * as moment from 'moment'
应该加载npm包提供的类型定义.
尽管如此,正如在/ moment/3319#issuecomment-263752265中说的那样,团队似乎在维护这些定义方面存在一些问题(他们仍在寻找维护它们的人).
你需要安装moment
没有--ambient
旗帜的打字.
然后使用它包括它 import * as moment from 'moment'
小智 49
你需要在TS中分别导入moment()函数和Moment类.
我在这里的打字稿文档中找到了一个注释.
/*〜注意ES6模块不能直接导出可调用函数.*〜此文件应使用CommonJS样式导入:*~import x = require('someLibrary');
因此,将时刻js导入typescript的代码实际上如下所示:
import { Moment } from 'moment'
....
let moment = require('moment');
...
interface SomeTime {
aMoment: Moment,
}
...
fn() {
...
someTime.aMoment = moment(...);
...
}
Run Code Online (Sandbox Code Playgroud)
siv*_*udh 14
通过打字
Moment.js现在支持v2.14.1中的TypeScript.
请参阅:https://github.com/moment/moment/pull/3280
直
可能不是最好的答案,但这是蛮力的方式,它对我有用.
moment.js
文件并将其包含在您的项目中.
$ tree
.
??? main.js
??? main.js.map
??? main.ts
??? moment.js
```
import * as moment from 'moment';
class HelloWorld {
public hello(input:string):string {
if (input === '') {
return "Hello, World!";
}
else {
return "Hello, " + input + "!";
}
}
}
let h = new HelloWorld();
console.log(moment().format('YYYY-MM-DD HH:mm:ss'));
Run Code Online (Sandbox Code Playgroud)
node
用来跑main.js
.NSj*_*nas 12
不知道什么时候改变了,但是使用最新版本的打字稿,你只需要使用import moment from 'moment';
,其他一切都应该正常工作.
ES6语法:
1.安装时刻
npm install --save moment
Run Code Online (Sandbox Code Playgroud)
2. 在你的打字稿文件中导入“时刻”
import moment from 'moment';
Run Code Online (Sandbox Code Playgroud)
我刚刚注意到我赞成和评论的答案是模棱两可的。所以以下正是对我有用的。我目前在 Moment2.26.0
和 TS 上3.8.3
:
在代码中:
import moment from 'moment';
Run Code Online (Sandbox Code Playgroud)
在 TS 配置中:
{
"compilerOptions": {
"esModuleInterop": true,
...
}
}
Run Code Online (Sandbox Code Playgroud)
我建立了两所以这个配置导入到其他配置文件CommonJS的和EMS。
洞察力来自与使用 Express 相关的答案。我认为值得在这里添加,以帮助任何与 Moment.js 相关的搜索,而不是更一般的东西。
归档时间: |
|
查看次数: |
79535 次 |
最近记录: |