你好,我用vuejs cli生成了一个项目(https://github.com/vuejs/vue-cli)。cli使用webpack,而im在vue文件中使用jquery时遇到问题。我总是得到一个。
http://eslint.org/docs/rules/no-undef '$' is not defined我已经尝试编辑我的webpack.dev.config以包含provide插件块,如下所示:
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文件对它有帮助,我会尝试在脚本块中使用console.log $,就像这样
<script>
export default {
name: 'how_can_we_help_you',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
}
}
console.log($)
</script>请帮助我非常陷入困境,并一直试图解决这个相当聪明的现在。提前谢谢。
发布于 2017-06-12 18:18:52
经过两天的搜寻。它的eslintrc.js
将此添加到以下代码中,它将修复provide插件。
env: {
browser: true,
jquery: true
},发布于 2017-06-12 18:17:56
进入main.js并执行
window.$ = window.jQuery = require('jquery')或者
Vue.use({
install: function(Vue, options){
Vue.prototype.$jQuery = require('jquery'); // you'll have this.$jQuery anywhere in your vue project
}
})发布于 2018-09-04 01:19:41
对于那些不使用webpack的人来说,另一种方法是使用expose-loader:
yarn add expose-loader在你的main.js中
import "expose-loader?$!jquery";https://stackoverflow.com/questions/44496906
复制相似问题