简体   繁体   English

为什么不将 jQuery 与 Vue.js 一起用于 AJAX?

[英]Why not use jQuery with Vue.js for AJAX?

I'm trying to optimize references on a greenfield project that uses Vue.js.我正在尝试优化使用 Vue.js 的绿地项目的引用。 It also uses Bootstrap, with requires jQuery for some of its magic, so jQuery is already in the mix.它还使用 Bootstrap,需要 jQuery 来实现它的一些魔法,所以 jQuery 已经在混合中了。 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?虽然我无意使用它的 DOM 操作工具,但 jQuery 已经存在了,所以有充分的理由不只是将它用于我的 Vue 组件中的 AJAX 调用吗? The syntax between jQuery and axios is fairly similar, so if I can avoid adding another library, that would be groovy. jQuery 和 axios 之间的语法非常相似,所以如果我可以避免添加另一个库,那就太好了。

The reasons behind the "don't use jQuery for AJAX in Vue" mantra are: “不要在 Vue 中为 AJAX 使用 jQuery”这一口号背后的原因是:

  • 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..如果$在组件中已经可用,则更倾向于查询或操作 DOM 而不是使用v-modelv-if:class等。
  • Axios is an excellent library for AJAX and uses less data than jQuery. Axios 是一个优秀的 AJAX 库,使用的数据比 jQuery 少。
  • Using jQuery could lead to a hard to maintain mix between jQuery plugins and Vue components.使用 jQuery 可能会导致难以维护 jQuery 插件和 Vue 组件之间的混合。

My recommendation is to write a simple api.js helper file with methods such as:我的建议是使用以下方法编写一个简单的api.js帮助文件:

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.这些方法可能在内部使用$.ajax ,但是当您升级到 Bootstrap 5(不需要 jQuery )时,您可以将实现更改为其他内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM