简体   繁体   English

如何将jQuery类型输入到打字稿

[英]How to import jquery typings to typescript

I realize that there are multiple question about this, but I'm having issues finding one that matches mine, maybe someone can point me in the right direction. 我意识到对此存在多个问题,但是我在寻找一个与我的匹配的问题时遇到了问题,也许有人可以指出我正确的方向。

In my Visual Studio project I have used package.json to download jquery typings into the node_modules folder: 在我的Visual Studio项目中,我使用package.json将jquery类型下载到node_modules文件夹中:

{
  "version": "1.0.0",
  "name": "asp.net",
  "private": true,
  "devDependencies": {
    "@syncfusion/ej2": "17.1.51",
    "@syncfusion/ej2-base": "17.1.49",
    "@syncfusion/ej2-data": "17.1.51",
    "@syncfusion/ej2-inputs": "17.1.50",
    "@syncfusion/ej2-buttons": "17.1.50",
    "@syncfusion/ej2-splitbuttons": "17.1.51",
    "@syncfusion/ej2-lists": "17.1.47",
    "@syncfusion/ej2-navigations": "17.1.49",
    "@syncfusion/ej2-popups": "17.1.50",
    "@syncfusion/ej2-charts": "17.1.51",
    "@syncfusion/ej2-calendars": "17.1.51",
    "@syncfusion/ej2-grids": "17.1.51",
    "@syncfusion/ej2-maps": "17.1.51",
    "@syncfusion/ej2-pdf-export": "17.1.48",
    "@syncfusion/ej2-svg-base": "17.1.48",
    "@syncfusion/ej2-file-utils": "17.1.47",
    "@syncfusion/ej2-compression": "17.1.47",
    "@types/jquery": "3.2.0"
  }
}

The actual project already uses jquery 2.1.4, but I'm thinking it shouldn't be too much of an issue if I got 3.2.0 as there wasn't a 2.1.4 available, is the types version was related to jquery script version? 实际的项目已经使用了jquery 2.1.4,但是我认为如果我有3.2.0,就不会有太大的问题,因为没有2.1.4,类型类型与jquery有关脚本版本?

My tsconfig.json has the following: 我的tsconfig.json具有以下内容:

{
  "compilerOptions": {
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "es2015",
    "module": "commonjs",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "baseUrl": "./",
    "paths": {
      "@syncfusion/ej2-base": [ "./node_modules/@syncfusion/ej2-base/dist/ej2-base.umd.min.js" ],
      "@syncfusion/ej2-buttons": [ "./node_modules/@syncfusion/ej2-buttons/dist/ej2-buttons.umd.min.js" ],
      "@syncfusion/ej2-popups": [ "./node_modules/@syncfusion/ej2-popups/dist/ej2-popups.umd.min.js" ],
      "@syncfusion/ej2-maps": [ "./node_modules/@syncfusion/ej2-maps/dist/ej2-maps.umd.min.js" ],
      "@types/jquery": [ "./node_modules/@types/jquery/index.d.ts" ]
    }
  },
  "exclude": [
    "wwwroot"
  ]
}

My page QBMapByState.aspx has the following javascript file QBMapByState.config.js: 我的页面QBMapByState.aspx具有以下JavaScript文件QBMapByState.config.js:

System.config({
paths: {
'npm:': './node_modules/',
'syncfusion:': '../node_modules/@syncfusion/',
'types:': '../node_modules/@types/'

},
map: {
app: '../scripts/QBMapping',

//Syncfusion packages mapping
"@syncfusion/ej2-base": "syncfusion:ej2-base/dist/ej2-base.umd.min.js",
"@syncfusion/ej2-data": "syncfusion:ej2-data/dist/ej2-data.umd.min.js",
"@syncfusion/ej2-inputs": "syncfusion:ej2-inputs/dist/ej2-inputs.umd.min.js",
"@syncfusion/ej2-buttons": "syncfusion:ej2-buttons/dist/ej2-buttons.umd.min.js",
"@syncfusion/ej2-splitbuttons": "syncfusion:ej2-splitbuttons/dist/ej2-splitbuttons.umd.min.js",
"@syncfusion/ej2-lists": "syncfusion:ej2-lists/dist/ej2-lists.umd.min.js",
"@syncfusion/ej2-navigations": "syncfusion:ej2-navigations/dist/ej2-navigations.umd.min.js",
"@syncfusion/ej2-popups": "syncfusion:ej2-popups/dist/ej2-popups.umd.min.js",
"@syncfusion/ej2-charts": "syncfusion:ej2-charts/dist/ej2-charts.umd.min.js",
"@syncfusion/ej2-calendars": "syncfusion:ej2-calendars/dist/ej2-calendars.umd.min.js",
"@syncfusion/ej2-grids": "syncfusion:ej2-grids/dist/ej2-grids.umd.min.js",
"@syncfusion/ej2-maps": "syncfusion:ej2-maps/dist/ej2-maps.umd.min.js",
"@syncfusion/ej2-pdf-export": "syncfusion:ej2-pdf-export/dist/ej2-pdf-export.umd.min.js",
"@syncfusion/ej2-svg-base": "syncfusion:ej2-svg-base/dist/ej2-svg-base.umd.min.js",
"@syncfusion/ej2-file-utils": "syncfusion:ej2-file-utils/dist/ej2-file-utils.umd.min.js",
"@syncfusion/ej2-compression": "syncfusion:ej2-compression/dist/ej2-compression.umd.min.js"
},
packages: {
'app': { main: 'QBMapByState.js', defaultExtension: 'js' }
}
});

System.import('app');

I'm not sure if I need to add anything in here in regards to the type jquery. 我不确定是否需要在此添加有关jquery类型的任何内容。

And in my page's typescript file QBMapByState.ts I do some imports including the jquery typing like so: 在页面的打字稿文件QBMapByState.ts中,我进行了一些导入,包括键入如下的jquery:

import { Maps } from "@syncfusion/ej2-maps";
import { RadioButton, CheckBox, Button, ChangeEventArgs } from '@syncfusion/ej2-buttons';
import * as $ from '@types/jquery';

All but the jquery is transpiling. 除了jQuery,其他所有代码都在转译。 I get the following error: 我收到以下错误:

Cannot import type declaration files. 无法导入类型声明文件。 Consider importing 'jquery' instead of '@types/jquery' 考虑导入'jquery'而不是'@ types / jquery'

How can I get this to work? 我该如何工作? Where am I going wrong? 我要去哪里错了?

Debug Errors: 调试错误: 在此处输入图片说明

As the error states, you shouldn't import $ from the jquery typings, you import it from jquery itself: 如错误所示,您不应该从jquery输入中导入$ ,而是从jquery本身中导入$

import $ from "jquery";

As for your other question, there are some breaking changes between jquery 3 and jquery 2, so I'd imagine you want to use the type definitions for your jquery 2 installation: 至于您的其他问题,jquery 3和jquery 2之间有一些重大更改 ,因此我想您想在jquery 2安装中使用类型定义:

npm install @types/jquery@2

You should add typeRoots to the tsconfig.json . 您应该将typeRoots添加到tsconfig.json

You can check it here how to use it. 您可以在此处查看如何使用它。

https://www.typescriptlang.org/docs/handbook/tsconfig-json.html#types-typeroots-and-types https://www.typescriptlang.org/docs/handbook/tsconfig-json.html#types-typeroots-and-types

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM