gpt4 book ai didi

javascript - 如何在 vuejs webpack cli 项目中包含 jquery?

转载 作者:搜寻专家 更新时间:2023-10-30 22:18:27 24 4
gpt4 key购买 nike

你好,我曾经使用 vuejs cli 生成一个项目(https://github.com/vuejs/vue-cli)。 cli 使用 webpack,我在 vue 文件中使用 jquery 时遇到问题。我总是得到一个。

http://eslint.org/docs/rules/no-undef  '$' is not defined

我已尝试编辑我的 webpack.dev.config 以包含提供插件 block ,如下所示:

var utils = require('./utils')
var webpack = require('webpack')
var config = require('../config')
var merge = require('webpack-merge')
var baseWebpackConfig = require('./webpack.base.conf')
var HtmlWebpackPlugin = require('html-webpack-plugin')
var FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')

// add hot-reload related code to entry chunks
Object.keys(baseWebpackConfig.entry).forEach(function (name) {
baseWebpackConfig.entry[name] = ['./build/dev-client'].concat(baseWebpackConfig.entry[name])
})

module.exports = merge(baseWebpackConfig, {
module: {
rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap })
},
// cheap-module-eval-source-map is faster for development
devtool: '#cheap-module-eval-source-map',
plugins: [
new webpack.DefinePlugin({
'process.env': config.dev.env
}),
// https://github.com/glenjamin/webpack-hot-middleware#installation--usage
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
// https://github.com/ampedandwired/html-webpack-plugin
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'index.html',
inject: true
}),
new FriendlyErrorsPlugin(),
new webpack.ProvidePlugin({
$ : "jquery",
jQuery : "jquery"
})
]
})

然而,当我尝试使用 jquery 时,我一次又一次地遇到同样的问题。我不反对为此使用 cdn,我真的只是无法让这个想法包含 jquery,无论我尝试什么。

如果 vue 文件有帮助,我会在脚本 block 内尝试 console.log $,就像这样

<script>
export default {
name: 'how_can_we_help_you',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
}
}
console.log($)
</script>

请帮助我陷入困境,现在已经尝试解决这个问题很长时间了。提前致谢。

最佳答案

经过两天的搜索。它的 eslintrc.js

将此添加到以下内容,它将修复提供插件。

env: {
browser: true,
jquery: true
},

关于javascript - 如何在 vuejs webpack cli 项目中包含 jquery?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44496906/

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