带有Typescript的Jquery:'ElementFinder'

时间:2016-06-03 04:18:16

标签: jquery typescript webpack typing

我正在尝试在我的angular2项目中使用jquery,它正在使用webpack进行构建。 我从这里得到了webpack angular2入门套件:  here

然而,由于我的构建失败,我无法弄清楚如何摆脱这些错误。

这是我的typings.json

     {
       "globalDependencies": {
         "angular-protractor": "registry:dt/angular-protractor#1.5.0+20160425143459",
         "angular2-beta-to-rc-alias": "file:./node_modules/@angularclass/angular2-beta-to-rc-alias/src/beta-17/typings.d.ts",
         "core-js": "registry:dt/core-js#0.0.0+20160317120654",
         "hammerjs": "registry:dt/hammerjs#2.0.4+20160417130828",
         "jasmine": "registry:dt/jasmine#2.2.0+20160505161446",
         "jquery": "github:DefinitelyTyped/DefinitelyTyped/jquery/jquery.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd",
         "node": "registry:dt/node#6.0.0+20160514165920",
         "selenium-webdriver": "registry:dt/selenium-webdriver#2.44.0+20160317120654",
         "source-map": "registry:dt/source-map#0.0.0+20160317120654",
         "uglify-js": "registry:dt/uglify-js#2.6.1+20160316155526",
         "webpack": "registry:dt/webpack#1.12.9+20160321060707"
       }
     }

我试过了:

   typings install dt~jquery --global --save

同样,但错误仍然存​​在:

      Property 'css' does not exist on type 'ElementFinder'.

并且:

      Subsequent variable declarations must have the same type.  Variable '$' must be of type 'cssSelectorHelper', but here has type 'JQueryStatic'.

1 个答案:

答案 0 :(得分:1)

你需要定义jQuery类型,我正在使用:

https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/7de6c3dd94feaeb21f20054b9f30d5dabc5efabd/jquery/jquery.d.ts

它对我来说很好。

我遇到了同样的问题,我不得不自己下载this打字并将其放在我的文件夹中。

然后在你的tsconfig.js

  "files": [
    "./src/app/**/*.ts",
    "./src/platform/**/*.ts",
    "!./node_modules/**",
    "./src/custom-typings.d.ts",
    "./src/jquery-typings-custom/index.d.ts",  !!!!!!! This is the folder where I put those typings downloaded from the url
    "./src/vendor/slick/slick-typings.d.ts",
    "./typings/index.d.ts"
  ],

然后,在你的一个组件中(可能是根组件,通常是app.ts)你应该这样做:

       declare var jQuery : JQueryStatic;

然后你不应该使用$,而应该开始使用jQuery(这个问题也有一个解决方法,但我并不在意)。