简体   繁体   English

如何在打字稿文件中加载jQuery模块?

[英]How to load jquery module in a typescript file?

To import the jquery library in my typescript project, I did the following. 要在我的打字稿项目中导入jquery库,我做了以下工作。

  • npm install typings --global npm安装类型--global
  • typings install dt~jquery --global --save 键入install dt〜jquery --global --save

Then in my typescript file, I did: 然后在我的打字稿文件中,我做了:

import {jquery as $} from 'jquery';

But the transcript compiler throws an error saying: 但是笔录编译器抛出一个错误,说:

error TS2307: Cannot find module 'jquery'.

在此处输入图片说明

What is that I am missing here? 我在这里想念什么? Why can the compiler not find the jquery module? 为什么编译器找不到jquery模块?

Typescript version I am using : 2.1.5 我正在使用的打字稿版本: 2.1.5

You still need to make the typescript compiler aware of your typings. 您仍然需要使打字稿编译器知道您的键入。

Any one of these should handle your issue. 这些中的任何一个都可以处理您的问题。 Don't do more than one or they may conflict. 不要做太多,否则它们可能会发生冲突。

  • Add "typings/index.d.ts" to your tsconfig.json 's files array "typings/index.d.ts"添加到tsconfig.json的文件数组中
  • Use ///<reference path... to pull in the jquery.d.ts file. 使用///<reference path...拉入jquery.d.ts文件。
  • Throw all of this away and use @types 扔掉所有这些并使用@types

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM