admin管理员组文章数量:1335615
I'm using typescript with jquery, but I keep getting
Uncaught TypeError: $ is not a function
Has anybody seen this before?
I'm piling typescript to ES2017, then transpiling to ES5 using webpack.
//tsconfig
{
"pileOnSave": true,
"pilerOptions": {
"module": "es2015",
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true,
"target": "es2017",
"noImplicitAny": false,
"outDir": "Output",
"esModuleInterop": true
}
}
How jquery is being used
import * as $ from "jquery";
var form = $(document.createElement('form'));
The browser sees jquery ($)
But then I get this
I'm using typescript with jquery, but I keep getting
Uncaught TypeError: $ is not a function
Has anybody seen this before?
I'm piling typescript to ES2017, then transpiling to ES5 using webpack.
//tsconfig
{
"pileOnSave": true,
"pilerOptions": {
"module": "es2015",
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true,
"target": "es2017",
"noImplicitAny": false,
"outDir": "Output",
"esModuleInterop": true
}
}
How jquery is being used
import * as $ from "jquery";
var form = $(document.createElement('form'));
The browser sees jquery ($)
But then I get this
Share Improve this question asked Feb 7, 2018 at 21:59 AnishAnish 3,1723 gold badges30 silver badges30 bronze badges 3- prntscr./iblrdv so there's something wrong with your jquery – An0num0us Commented Feb 7, 2018 at 22:03
-
is
jquery
the path of your JQuery Library?? you have to type the path of your JQuery library – Kenry Sanchez Commented Feb 7, 2018 at 22:03 - stackoverflow./questions/28969861/… – An0num0us Commented Feb 7, 2018 at 22:06
2 Answers
Reset to default 9Found the solution. To use $ as a function, I had to import the default from the the jquery npm module. With this import statement, it works.
import $ from "jquery";
I also had to turn this on in tsconfig.json
"allowSyntheticDefaultImports": true
Seems like you are missing jQuery type definitions which are needed by typescript piler. You need to install both jQuery and jQuery type definitions. If you are using npm
to install packages, the below steps will work. (Otherwise make sure you have both the packages installed in your environment)
1.Install jQUery
npm install --save jquery
2.Install jQuery type definitions
npm install -D @types/jquery
Now, you can import jQuery in your code as below
import * as $ from 'jquery';
本文标签: javascriptTypescript and Jquery import is not a functionStack Overflow
版权声明:本文标题:javascript - Typescript and Jquery import - $ is not a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742392186a2466208.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论