Uncaught ReferenceError: $ is not defined (VueJS)

Enrique Bermúdez picture Enrique Bermúdez · Jan 30, 2018 · Viewed 33.6k times · Source

I've started a VueJS project with:

vue init webpack my-project

And got jQuery with npm:

npm install jquery

And i put this line on my main.js file:

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

Either way, i can't use this piece of code: (from semantic ui)

$('.ui.rating')
  .rating()
;

Because i get this error:

Uncaught ReferenceError: $ is not defined

Any idea why this is happening ?

Answer

samayo picture samayo · Jan 30, 2018

If you have jQuery installed via npm, just import it like this:

import $ from 'jquery'

And inside your methods, you can start using $ as:

methods: {
  getFoo() {
    $( "div.foo" ).html();
  }
}