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 ?
The $ is not defined ReferenceError usually arises when jQuery is not loaded and JavaScript is not recognizing the $ symbol. To solve this error, first, use jQuery CDN link inside the head section or download the jQuery file and use the jQuery file link inside the head section.
Uncaught ReferenceError: $ is not defined This is error is most of the time related to not having installed Jquery or not having it declared properly. You may need to do require('jquery'). default but don't quote me on that. You should really be using import here I think, though.
Reference errors in Javascript are mainly thrown when an attempt is made to reference a variable that does not exist or is out of scope. Therefore, in the majority of cases, a ReferenceError can be fixed by making sure that the referenced variable is defined correctly and is being called in the correct scope.
The most common reason behind the error "Uncaught ReferenceError: $ is not defined" is executing the jQuery code before the jQuery library file has loaded. Therefore make sure that you're executing the jQuery code only after jQuery library file has finished loading.
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With