Skip to content

Instantly share code, notes, and snippets.

@ansidev
Last active October 14, 2024 14:39
Show Gist options
  • Save ansidev/46a715dd319cef546646383eac13ec5f to your computer and use it in GitHub Desktop.
Save ansidev/46a715dd319cef546646383eac13ec5f to your computer and use it in GitHub Desktop.
Use jQuery with NuxtJS
const webpack = require('webpack')
module.exports = {
// other configs
build: {
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery'
})
]
},
// other configs
}
{
// other configs.
"dependencies": {
// Replace <jquery.version> with the jquery version you want.
"jquery": "<jquery.version>"
},
// other configs.
}
@ansidev
Copy link
Author

ansidev commented Mar 10, 2020

In the webpack v4, the vendor option was removed

I 've updated.

@ansidev
Copy link
Author

ansidev commented Mar 10, 2020

Save jquery.min.js

modify nuxt.config.js

'plugins': [
     { src: '~/plugins/jquery.min.js', ssr: false }
],

You should install jquery via npm/yarn. You can update it easier.

@thamerbelfkihthamer
Copy link

there is any way to use jquery within Vuejs components with $, please?

@cokobware
Copy link

there is any way to use jquery within Vuejs components with $, please?

Try this and see if it helps... https://forum.vuejs.org/t/how-to-use-jquery-in-vue-component/45000/5

@SajjadGazery
Copy link

in Nuxt3

First you need to install jquery in your project

npm i jquery

Then in the file ~/plugins/jquery.js add the following codes:

import $ from 'jquery'
window.jQuery = window.$ = $
export default jQuery;

And finally add the following code to the nuxt.config.ts

plugins: [
  { src: "~/plugins/jquery", mode: "client" },
 ],

Sample

<script setup>
   onMounted(()=>{
      console.log($('body'));
   })
</script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment