在vue项目中,将juery设置为全局变量的方法
1.首先执行:npm install jQuery --save-dev,在package.json里加入jQuery。
2.修改build下的webpack.base.conf.js
方法一:
首先加入:
const webpack = require("webpack")
然后在module.exports 对象中加入:
plugins: [ new webpack.optimize.CommonsChunkPlugin('common.js'), new webpack.ProvidePlugin({ jQuery: "jquery", $: "jquery" }) ]
方法二:
先加入:
const webpack = require("webpack")
然后:
resolve: { extensions: ['.js', '.vue', '.json'], alias: { 'vue$': 'vue/dist/vue.esm.js', '@': resolve('src'), 'jquery': 'jquery' } },
最后:
plugins: [ new webpack.ProvidePlugin({ jQuery: "jquery", $: "jquery" }) ]
3.在app.vue中调用jquery
mounted: function() { console.log($); console.log($('#ss')[0].innerHTML); },
打印结果:
相关推荐
85477104 2020-11-17
84901334 2020-07-28
gloria0 2020-10-26
gloria0 2020-08-09
hline 2020-07-29
不知道该写啥QAQ 2020-07-18
yuzhu 2020-11-16
KANSYOUKYOU 2020-11-16
sjcheck 2020-11-03
怪我瞎 2020-10-28
源码zanqunet 2020-10-28
王军强 2020-10-21
学习web前端 2020-09-28
QiaoranC 2020-09-25
anchongnanzi 2020-09-21
安卓猴 2020-09-12
Macuroon 2020-09-11
kiven 2020-09-11
LittleCoder 2020-09-11