将Jquery添加到Vue-Cli 3

非数值浮点数

我目前正在尝试将Jquery添加到我的vue-cli项目中。我知道它可能产生的不当行为,但是无论如何;由于build/webpack.base.conf.js不再存在,因此我尝试vue.config.js通过添加以下内容进行编辑

 module.exports {
    ...
    chainWebpack: config => {
    config.plugin('define').tap(definitions => {
      definitions[0] = Object.assign(definitions[0], {
        $: 'jquery',
        jquery: 'jquery',
        'window.jQuery': 'jquery',
        jQuery: 'jquery',
        _: 'lodash'
      })
      return definitions
    })
  }
   ...
 }

要么

const webpack = require('webpack')

module.exports {
   ...
 plugins: [
  new webpack.ProvidePlugin({
     $: 'jquery',
     jquery: 'jquery',
     'window.jQuery': 'jquery',
     jQuery: 'jquery'
   })
  ]
   ...
 }

这两个选项似乎都不起作用。对于#1,似乎什么都没有发生,对于#2,我得到了编译错误。不允许使用“插件”或无法解析“ ProvidePlugin”,当我尝试直接在main.js中导入jQuery并定义$运算符时,尝试使用它时jquery仍未定义。

预先大谢谢!

非数值浮点数

通过添加解决main.js

window.$ = window.jQuery = require('jquery');

这对我来说是成功的,jQuery现在可以在全球范围内使用

另一种方法是:

Vue.use({
install: function(Vue, options){
    Vue.prototype.$jQuery = require('jquery'); // you'll have this.$jQuery anywhere in your vue project
    }
});

我希望这将有助于将来解决同一问题的人。如果仍然无法解决问题,请检查此问题或查看文档

编辑:确保您已运行npm install jquery

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章