Jquery 使用 Typescript:属性 'css' 在类型 'ElementFinder' 上不存在

Jquery with Typescript : Property 'css' does not exist on type 'ElementFinder'

我正在尝试在我的 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'.

您需要定义 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(这个问题也有解决方法,但我不在乎)。