【问题标题】:Jquery with Typescript : Property 'css' does not exist on type 'ElementFinder'带有 Typescript 的 Jquery:“ElementFinder”类型上不存在属性“css”
【发布时间】:2016-10-03 00:23:42
【问题描述】:

我正在尝试在使用 webpack 构建的 angular2 项目中使用 jquery。 我从这里得到了 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 typescript webpack typing


    【解决方案1】:

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

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

    它对我来说很好用。

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

    然后在你的 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 而不是使用 $ (这个问题也有解决方法,但我不在乎)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-14
      • 1970-01-01
      • 2023-04-02
      • 2018-08-17
      • 1970-01-01
      • 2023-02-06
      相关资源
      最近更新 更多