简体   繁体   English

如何在VS2017中使用TypeScript的jQuery

[英]How can I use jQuery from TypeScript with VS2017

NOTE : this is a question about Visual Studio 2017 . 注意 :这是关于Visual Studio 2017的问题。 Answers or advice relating to earlier versions are likely to be un helpful. 与早期版本相关的答案或建议可能没有帮助。 Thanks. 谢谢。


I'm utterly at my wit's end. 我完全在我的智慧结束。 I've been trying to get a simple Visual Studio 2017 web project to work using TypeScript (v2.3), and it's so stupidly frustrating that it has left me exhausted and enraged. 我一直在尝试使用TypeScript(v2.3)来创建一个简单的Visual Studio 2017 Web项目,这让我感到非常沮丧,因为它让我感到疲惫和愤怒。 I truly doubt that anyone has ever accomplished this feat without abandoning the Visual Studio tooling and setting everything up outside/alongside VS. 我真的怀疑任何人都没有完成这项壮举而不放弃Visual Studio工具并在VS外面/旁边设置所有内容。

My first problem is that there are various resources on the web that suggest different ways to do this, and they all conflict: "use Gulp", "no, use Bower", "use NPM", "no, use WebPack". 我的第一个问题是网上有各种各样的资源,提出了不同的方法,它们都有冲突:“使用Gulp”,“不,使用Bower”,“使用NPM”,“不,使用WebPack”。 And of course much of the advice to be found relates to earlier versions of Visual Studio. 当然,很多建议都与早期版本的Visual Studio有关。

Similarly, the TypeScript syntax for importing modules seems to change with every version, so much of that advice is also out-of-date, and further depends on which module loader, etc. you're using. 同样,对于导入模块的打字稿语法似乎与每一个版本的改变, 以至于建议也是出于过期,进一步取决于哪个模块加载器,等你使用。

Lots of options is "great", but the tooling must be at least slightly opinionated, and I can't figure out what the path of least resistance is. 很多选择都是“很棒的”,但工具必须至少有点自以为是,我无法弄清楚阻力最小的路径是什么。 I'd like to stick with the stuff that MS have actually integrated with VS, since I'm really not a fan of command lines and config files when the commands and config formats change weekly. 我想坚持MS实际上与VS集成的东西,因为当命令和配置格式每周更改时,我真的不喜欢命令行和配置文件。

I followed the steps in the TypeScript handbook to create a web project that does little more than say "hello world", but stopped just before the section "Add Angular 2". 我按照TypeScript手册中的步骤创建了一个Web项目,它只是说“hello world”,但在“Add Angular 2”部分之前停止了。 (I don't want to use Angular, or at least not yet). (我不想使用Angular,或者至少还没有使用)。

So, by this point I've got some NuGet dependencies, some NPM dependencies, and package.json , tsconfig.json , and gulpfile.json files. 所以,到目前为止,我有一些NuGet依赖项,一些NPM依赖项,以及package.jsontsconfig.jsongulpfile.json文件。

What I want to do now is add jQuery to my project, and be able to say (in my TypeScript file): 我现在要做的是将jQuery添加到我的项目中,并且能够说(在我的TypeScript文件中):

let foo: JQuery = $("p");

So, I obviously need the jQuery library, and I need typescript definitions for jQuery. 所以,我显然需要jQuery库,我需要jQuery的typescript定义。 That should not be hard - but I, a developer of some 30 years standing, cannot figure out how to add those things using the new tooling. 这应该不难 - 但我,一个有30年历史的开发人员,无法弄清楚如何使用新工具添加这些东西。

I tried adding "jquery" as a dependency in the NPM package.json file: 我尝试在NPM package.json文件中添加“jquery”作为依赖项:

"dependencies": {
  "jquery": "^3.2.1"
},

... but I can't figure out how to add the corresponding typescript definitions. ...但我无法弄清楚如何添加相应的打字稿定义。

The TypeScript manual suggests using npm import @types/jquery but I don't have an NPM command line, and when I tried adding that directly to the dependencies (or devDependencies) section in the package.json file, it didn't recognize it. TypeScript手册建议使用npm import @types/jquery但我没有NPM命令行,当我尝试将它直接添加到package.json文件中的依赖项(或devDependencies)部分时,它无法识别它。

In yet another "tutorial" I found (this one using Bower just to make sure it was a little more confusing), it was suggested to add the following to the tsconfig.json file to enable automatic type aquisition for TypeScript, which sounds great: 在我发现的另一个“教程”中(这个使用Bower只是为了确保它有点混乱),建议将以下内容添加到tsconfig.json文件中以启用TypeScript的自动类型获取,这听起来很棒:

"typeAcquisition": {
  "enable": true
},

But sadly that doesn't seem to work. 但遗憾的是,这似乎不起作用。 Or at least, not with the setup I currently have. 或者至少,不是我目前的设置。 [While it did provide Intellisense within the IDE when I followed the steps in that other tutorial, I was never actually able to get the project to compile.] [虽然当我按照其他教程中的步骤时它确实在IDE中提供了Intellisense,但我实际上从来没有能够让项目进行编译。]

So, here I am, begging for help. 所以,我在这里,求助于你。 How can I get my one-line piece of TypeScript to (a) recognize the JQuery type definitions, and (b) actually compile and run? 我怎样才能得到我的单行TypeScript来(a)识别JQuery类型定义,(b)实际编译和运行?

Better yet, if anyone knows of any online resources that actually explain all this for the current version of all the tools then I'd love to see that. 更好的是,如果有人知道任何在线资源实际上解释了当前版本的所有工具的所有这些,那么我很乐意看到这一点。 Otherwise I may just go back to VS2015. 否则我可能会回到VS2015。

I had difficults with adding typing support to VS2017 but it looks like 15.4 update fix my problems. 我在向VS2017添加打字支持方面遇到了困难,但看起来像15.4更新解决了我的问题。 First of all you should add @types/jquery devDependencies to your package.json file. 首先,您应该将@types/jquery devDependencies添加到package.json文件中。 For example: 例如:

{
  "name": "asp.net",
  "version": "0.0.0",

  "dependencies": {
    "jquery": "3.1.1",
    /*other packages*/
  },

  "devDependencies": {
    "gulp": "3.8.11",        
    "@types/jquery": "2.0.47"
    /*other dev packages*/
  }
}

Than you should setup your tsconfig.json file. 比你应该设置你的tsconfig.json文件。 For example: 例如:

{
  "compilerOptions": {
    "module": "none",
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noEmitOnError": true,
    "removeComments": true,
    "sourceMap": true,
    "types": [
      "jquery"
    ],
    "lib": [
      "dom",
      "es5",
      "scripthost",
      "es2015.iterable"
    ],
    "target": "es5",
    //"outDir": "wwwroot/app-out"
    "outFile": "wwwroot/app-out/app.js"
  },
  "compileOnSave": true,
  "exclude": [
    "node_modules",
    "wwwroot"
  ]
}

So I set module to none so it does not require to set up module bundler such as webpack for example. 所以我将module设置为none因此不需要设置模块捆绑器,例如webpack。 In types we should specify jquery which we specified early in package.json . 在类型中,我们应该指定我们在package.json提前指定的jquery Also we should specify "dom", "es5", "scripthost", "es2015.iterable" in lib because @types/jquery require it. 我们还应该在lib指定"dom", "es5", "scripthost", "es2015.iterable" ,因为@types/jquery需要它。 outFile tell ts where to place compiled js file. outFile告诉ts放置编译的js文件的位置。 So after that you can just add reference to it in your page. 因此,您可以在页面中添加对它的引用。

After that jquery types start working in ts file without any further set up. 之后,jquery类型开始在ts文件中工作而无需进一步设置。 Sometimes tsconfig.json file changes does not apply on fly. 有时tsconfig.json文件更改不适用于fly。 So make changes, download npm modules (rebuild solution for example) and then restart VS. 所以进行更改,下载npm模块(例如重建解决方案),然后重新启动VS.

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

相关问题 如何在VS2017中使用通过NuGet安装的Typescript定义(typings)文件? - How do I use Typescript definition (typings) files installed via NuGet in VS2017? 如何在 VS2017 中抑制虚假的 Typescript 编译错误? - How can I suppress spurious Typescript compilation errors in VS2017? ASP MVC VS2017 AdminLTE 2.4.8无法建立无法找到TypeScript - ASP MVC VS2017 AdminLTE 2.4.8 Can't build can't locate TypeScript 在VS2017中调试Aurelia webpack typescript应用程序 - Debug Aurelia webpack typescript application in VS2017 在vs2017上安装node.js打字稿包 - installing node.js typescript packages on vs2017 VS2017中TypeScript文件的代码格式 - Code-formatting in VS2017 for TypeScript files Typescript 指令错误:VS2017 中的“断点设置但尚未绑定” - Error on Typescript instruction: “Breakpoints set but not yet bound” in VS2017 VS2017需要很长时间才能在保存时重新编译打字稿 - VS2017 takes long time recompiling typescript on save VS2017,ASP.net,Typescript和npm - VS2017, ASP.net, Typescript, and npm 使用VS2017编译,捆绑和缩小打字稿到JavaScript - Compile, bundle and minify typescript into JavaScript using VS2017
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM