带有Typescript的Jquery:类型"ElementFinder"上不存在属性"css" [英] Jquery with Typescript : Property 'css' does not exist on type 'ElementFinder'

查看:264
本文介绍了带有Typescript的Jquery:类型"ElementFinder"上不存在属性"css"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的angular2项目中使用jquery,该项目正在使用webpack进行构建. 我从这里获得了webpack angular2入门套件: 此处

I'm trying to use jquery in my angular2 project which is getting build with webpack . I got the webpack angular2 starter kit from here : here

但是,由于这些错误导致我的构建失败,因此我无法弄清楚如何消除这些错误.

However ,I can't figure out how to get rid of these errors as my build is failing because of them .

这是我的Types.json

This is my 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

同样,但错误仍然在这里:

As well , but the error is still here :

      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类型,我正在使用:

You'll need to define jQuery typings, I'm using :

https://raw.githubusercontent.com/DefinitelyTyped/确定输入/7de6c3dd94feaeb21f20054b9f30d5dabc5efabd/jquery/jquery.d.ts

对我来说很好.

我遇到了同样的问题,不得不下载键入自己的内容,然后将其放在我的文件夹中.

I had the same issue and I had to download this typings myself and put it in my folders somewhere .

然后在您的tsconfig.js中

Then in your 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)中,您应该执行以下操作:

And then , in one of your components ( probably the root one , which is normally app.ts ) you should do :

       declare var jQuery : JQueryStatic;

然后您应该开始使用jQuery(也可以使用此方法解决此问题,但我不在乎),而不是使用$.

And then instead of using $ , you should start using jQuery(There is a workaround for this issue as well , but I didn't care ) .

这篇关于带有Typescript的Jquery:类型"ElementFinder"上不存在属性"css"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆