Using jquery in a vue project

1. NPM install jQuery, run the following code in the project root directory

npm install jquery --save

2. webpack configuration
Find the webpack.base.conf.js file in the build directory under the project root directory, and use the following code at the beginning to import webpack, because this file is not referenced by default,

var webpack = require('webpack')


然后在module.exports中添加一段代码,


// 原有代码 resolve: { extensions: ['.js', '.vue', '.json'], alias: { 'vue$': 'vue/dist/vue.esm.js', '@': resolve('src') } }, // 添加代码 plugins: [ new webpack.ProvidePlugin({ $: "jquery", jQuery: "jquery", jquery: "jquery", "window.jQuery": "jquery" }) ], // 原有代码 module: { rules: [ // ...... ] }


Import jQuery in main.js

import 'jquery'


最后启动项目 npm run dev即可

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324938773&siteId=291194637