Javascript Typescript和Jquery导入-$不是函数
我在jquery中使用typescript,但我一直在 未捕获的TypeError:$不是函数 以前有人见过这个吗 我正在将typescript编译到ES2017,然后使用webpack传输到ES5Javascript Typescript和Jquery导入-$不是函数,javascript,jquery,typescript,npm,Javascript,Jquery,Typescript,Npm,我在jquery中使用typescript,但我一直在 未捕获的TypeError:$不是函数 以前有人见过这个吗 我正在将typescript编译到ES2017,然后使用webpack传输到ES5 //tsconfig { "compileOnSave": true, "compilerOptions": { "module": "es2015", "removeComments": true, "preserveCon
//tsconfig
{
"compileOnSave": true,
"compilerOptions": {
"module": "es2015",
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true,
"target": "es2017",
"noImplicitAny": false,
"outDir": "Output",
"esModuleInterop": true
}
}
jquery的使用方式
import * as $ from "jquery";
var form = $(document.createElement('form'));
浏览器会看到jquery($)
但我明白了
似乎缺少typescript编译器所需的jQuery类型定义。您需要同时安装jQuery和jQuery类型定义。如果您正在使用
npm
安装软件包,以下步骤将起作用。(否则,请确保在您的环境中安装了这两个软件包)
1.安装jQUery
npm install --save jquery
2.安装jQuery类型定义
npm install -D @types/jquery
现在,您可以在代码中导入jQuery,如下所示
import * as $ from 'jquery';
找到了解决办法。要使用$作为函数,我必须从jQueryNPM模块导入默认值。有了这个import语句,它就可以工作了
import $ from "jquery";
我还必须在tsconfig.json中打开它
"allowSyntheticDefaultImports": true
因此,您的jquery出现了一些问题,jquery库的路径是
jquery
??您必须键入JQuery库的路径否——他有一个运行时错误。类型声明不会影响运行时代码。通过这种方法,我得到了未捕获的语法错误:请求的模块“../lib/jquery/dist/jquery.js”没有提供名为“default”的导出*但我没有通过npm
安装jquery,这可能是原因。