【问题标题】:How to import jQuery with TypeScript so that I can set it as a type for a property如何使用 TypeScript 导入 jQuery,以便我可以将其设置为属性的类型
【发布时间】:2018-10-23 07:37:57
【问题描述】:

我在我的 TypeScript 项目中使用 jQuery。我之前有npm install --save-dev @types/jquery。在我的文件中我有import $ from 'jquery'; 然后我有课:

class A {
  $el: $
}

PhpStorm 产生错误Cannot find name $。但是,如果我使用 jQuery 作为 $('selector') 之类的构造函数,则不会发生错误。

tsconfig.json:

{
  "compilerOptions": {
    "lib": ["es2018", "dom"],
    "baseUrl": "./src2",
    "allowSyntheticDefaultImports": true,
    "moduleResolution": "node",
    "types": [
      "node"
    ],
    "typeRoots": [
      "node_modules/@types"
    ]
  }

我尝试导入jQuery 也喜欢这个import * as $ from 'jquery'import $ = require('jquery');

jQuery 版本:3.3.1

【问题讨论】:

标签: jquery typescript phpstorm


【解决方案1】:

按照此处的答案,使用 JQuery 对象类型而不是 $ 进行变量/属性分配

https://stackoverflow.com/a/13153423/885626

【讨论】:

    猜你喜欢
    • 2021-10-23
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-01
    • 2019-03-09
    • 1970-01-01
    相关资源
    最近更新 更多