Moment.js + TypeScript.找不到名字的时刻 [英] Moment.js + TypeScript. Cannot find name moment

查看:144
本文介绍了Moment.js + TypeScript.找不到名字的时刻的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Studio中开发了带有剔除功能的Web应用程序.我刚刚通过Bower安装了敲除程序,在项目中包含了d.ts文件,在html页面中包含了脚本,现在我可以访问ko.

I develope web application with knockout in VisualStudio. I just installed knockout via bower, included d.ts file in project, included script to html page and now i can get access to ko.

现在,我尝试使用moment.js.就像敲除一样:安装,包括d.ts,包括脚本到页面,我得到一个错误cannot find name 'moment'.添加对d.ts的引用无济于事,import * as moment from 'moment'出现错误can not find module moment.

Now i try to use moment.js. Like with knockout: install, include d.ts, include script to page and i get an error cannot find name 'moment'. Adding reference to d.ts does not help, import * as moment from 'moment' get an error can not find module moment.

我知道这是一个愚蠢的问题,但我无法解决.我在做什么错了?

I know that it's a stupid problem, but i can't fix it. What am i doing wrong?

推荐答案

我建议使用某种工具来管理您的定义.一些受欢迎的选项(您不需要两者,只需选择一个):

What I'd recommend is using some tool for managing your definitions. Some popular options (you don't need both, just pick one):

  1. tsd-npm i -g tsd
  2. typings-npm i -g typings
  1. tsd - npm i -g tsd
  2. typings - npm i -g typings

这些工作与程序包管理器类似.您可以像安装npm/bower那样安装您的依赖项.

These work in a similar fashion as package managers. You can install your definitions like npm/bower installs your dependencies.

一旦您安装了其中之一,请转到您的项目并安装一下它及其定义

Once you have one of these installed, go to your project and install moment + its definition

npm install moment --save

其中之一:

tsd install moment --save
typings install moment --save --ambient

这两个方法都将创建一个文件夹,其中包含您的定义(都称为键入),并且两者中都包含一个"umbrella"定义文件,您应该在应用程序的入口点引用该文件(首先用于tsd ,第二次输入):

Both of these will create a folder with your definitions in it (both call it typings), and both have an "umbrella" definition file in it, which you should reference in the entry point of your application (first is for tsd, second for typings):

/// <reference path="typings/tsd.d.ts" />
/// <reference path="typings/index.d.ts" />

完成此操作后,您可以按以下方式使用动量(或其他任何模块):

After this is done, you can use moment (or any other module) as you would:

import * as moment from 'moment'
moment.isDate("I'm not a date")

我建议您检查以下内容:

I suggest checking out these:

https://github.com/DefinitelyTyped/tsd
https://github.com/typings/typings

这篇关于Moment.js + TypeScript.找不到名字的时刻的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆