我正在尝试使用 webpack 在 typescript 中导入 jquery。这就是我所做的。
npm init -y
npm install -g webpack
npm install ts-loader --save
touch webpack.config.js
在文件中,我这样写
module.exports = {
entry: './app.ts',
output: {
filename: 'bundle.js'
},
resolve: {
extensions: ['', '.webpack.js', '.web.js', '.ts', '.js']
},
module: {
loaders: [
{ test: /\.ts$/, loader: 'ts-loader' }
]
}
}
然后创建app.ts
并写入以下内容
import $ = require('jquery');
然后我npm install jquery --save
加载jquery组件。
然后当我执行 webpack
时,它给我找不到模块 'jquery' 消息。
ts-loader: Using typescript@1.6.2
Hash: af60501e920b87c93349
Version: webpack 1.12.2
Time: 1044ms
Asset Size Chunks Chunk Names
bundle.js 1.39 kB 0 [emitted] main
+ 1 hidden modules
ERROR in ./app.ts
(1,20): error TS2307: Cannot find module 'jquery'.
谁能告诉我我做错了什么?
最佳答案
Can somebody tell me what I did wrong?
您需要 jquery 定义:
tsd install jquery --save
你还需要一个 tsconfig.json:
tsc -init
将为您生成一个。但我建议:
{
"compilerOptions": {
"module": "commonjs",
"target": "es5"
},
"exclude": [
"node_modules"
]
}
还需要安装 typescript
npm install typescript -save
关于jquery - webpack 无法加载模块 jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33360079/