gpt4 book ai didi

javascript - Typescript 和 Jquery 导入 - $ 不是函数

转载 作者:行者123 更新时间:2023-12-03 02:32:33 24 4
gpt4 key购买 nike

我正在使用 typescript 和 jquery,但我不断得到

Uncaught TypeError: $ is not a function

有人见过这个吗?

我正在将 typescript 编译为 ES2017,然后使用 webpack 转换为 ES5。

//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($) enter image description here

但后来我明白了

enter image description here

最佳答案

找到了解决方案。要使用 $ 作为函数,我必须从 jquery npm 模块导入默认值。有了这个 import 语句,它就可以工作了。

import $ from "jquery";

我还必须在 tsconfig.json 中打开此功能

"allowSyntheticDefaultImports":  true

关于javascript - Typescript 和 Jquery 导入 - $ 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48673975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com