'How to install jQuery into Nuxt.js?
I'm trying to add jQuery in my project although i get an error that it is not defined
plugins: [
{ src: '~/plugins/js/svgSprite.js', mode: 'client' },
{ src: '~/plugins/vendor/jquery/jquery.min.js', mode: 'client' },
{ src: '~/plugins/vendor/bootstrap/js/bootstrap.bundle.min.js', mode: 'client' },
{ src: '~/plugins/vendor/bootstrap-select/js/bootstrap-select.min.js', mode: 'client' }, <-- gives me error
{ src: '~/plugins/vendor/magnific-popup/jquery.magnific-popup.min.js', mode: 'client' },
{ src: '~/plugins/vendor/smooth-scroll/smooth-scroll.polyfills.min.js', mode: 'client' },
{ src: '~/plugins/vendor/object-fit-images/ofi.min.js', mode: 'client' },
{ src: '~/plugins/js/theme.js', mode: 'client' }
],
Why is this happening, I'm obviously declaring jQuery before bootstrap-select.
Solution 1:[1]
I do not recommend adding jQuery to a Nuxt project.
But if you really want to, you can follow those steps:
- install it with
yarn add jquery - add those to your
nuxt.config.jsfile
import webpack from 'webpack'
export default {
// ...
build: {
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery'
})
]
},
}
- be sure that you do have the following in your
.eslintrc.jsfile
module.exports = {
// ...
env: {
// ...
commonjs: true,
jquery: true
},
}
Then, you can use it like this in a method or alike
$("h2").addClass("tasty-class")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | kissu |
