4

I'm trying to optimize references on a greenfield project that uses Vue.js. It also uses Bootstrap, with requires jQuery for some of its magic, so jQuery is already in the mix. While I have no intention of using its DOM manipulation tools, jQuery is already there, so is there a good reason to not just use it for the AJAX calls from within my Vue components? The syntax between jQuery and axios is fairly similar, so if I can avoid adding another library, that would be groovy.

4
  • 5
    Have you considered just using vanilla fetch? Commented Mar 2, 2019 at 14:49
  • 1
    IMO, if jQuery is required and utilised by your app. There is no real reason to include another library to handle AJAX. @SebastianSpeitel beat me to it! Use fetch and polyfill with jQuery - caniuse.com/#feat=fetch Commented Mar 2, 2019 at 14:53
  • @SebastianSpeitel yes, but I'd have to polyfill for that anyway for compatibility which seems like a half-step. Commented Mar 2, 2019 at 14:54
  • jQuery and Vue.js sounds bad together because they are solution of different generation. You should use fetch and drop jQuery by default because it is obselete, especially in new project. Commented Mar 2, 2019 at 23:56

1 Answer 1

4

The reasons behind the "don't use jQuery for AJAX in Vue" mantra are:

  • If $ is already available in the component, it's more tempting to query or manipulate the DOM instead of using v-model, v-if, :class, etc..
  • Axios is an excellent library for AJAX and uses less data than jQuery.
  • Using jQuery could lead to a hard to maintain mix between jQuery plugins and Vue components.

My recommendation is to write a simple api.js helper file with methods such as:

api.get(url) 
api.post(url, data)
...etc.

These methods may use $.ajax internally, but when you'll upgrade to Bootstrap 5 (which doesn't require jQuery) you can change the implementation to something else.

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, I get all of that, but Bootstrap today uses jQuery so it's already there. BS5 doesn't even have a roadmap or active dev yet.
Aside from the objections above, I don't see an issue with using jQuery.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.