如何使用 TypeScript 导入 jQuery 以便我可以将其设置为 属性 的类型
How to import jQuery with TypeScript so that I can set it as a type for a property
我在我的 TypeScript 项目中使用 jQuery
。我之前有 npm install --save-dev @types/jquery
。在我的文件中我有 import $ from 'jquery';
然后我有 class:
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
对象类型而不是 $
进行 variable/property 赋值
我在我的 TypeScript 项目中使用 jQuery
。我之前有 npm install --save-dev @types/jquery
。在我的文件中我有 import $ from 'jquery';
然后我有 class:
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
对象类型而不是 $
进行 variable/property 赋值