简体   繁体   English

安装 jQuery Rails 5.1 Yarn 还是 Gem?

[英]Install jQuery Rails 5.1 Yarn or Gem?

if I want to install jQuery on Rails 5.1, what is the differences between install it with:如果我想在 Rails 5.1 上安装 jQuery,安装它与以下有什么区别:

yarn add jquery

or

gem 'jquery-rails'

Or maybe I have to do both?或者也许我必须两者都做?

The gem jquery-rails and jQuery are different. gem jquery-railsjQuery是不同的。 Read the README on jquery-rails repository and you will see what the gem offers.阅读jquery-rails存储库上的自述文件,您将看到 gem 提供的内容。

If you just want jQuery on your project and you are confortable with yarn, use yarn.如果您只想在您的项目中使用 jQuery 并且您对 yarn 感到满意,请使用 yarn。

If you want the features that the gem offers, use the gem.如果您想要 gem 提供的功能,请使用 gem。

Look at these questions, they can help you.看看这些问题,它们可以帮助你。

Why use gems for serving assets instead of the vendor file? 为什么使用 gems 来提供资产而不是供应商文件?

Bootstrap gem in Rails Rails 中的 Bootstrap gem

You don't need to use jquery-rails on Rails 5.1.您不需要在 Rails 5.1 上使用jquery-rails

From http://guides.rubyonrails.org/5_1_release_notes.html来自http://guides.rubyonrails.org/5_1_release_notes.html

2.3 jQuery no longer a default dependency 2.3 jQuery 不再是默认依赖

jQuery was required by default in earlier versions of Rails to provide features like data-remote, data-confirm and other parts of Rails' Unobtrusive JavaScript offerings.默认情况下,早期版本的 Rails 需要 jQuery 来提供数据远程、数据确认和 Rails 的 Unobtrusive JavaScript 产品的其他部分等功能。 It is no longer required, as the UJS has been rewritten to use plain, vanilla JavaScript.不再需要它,因为 UJS 已经被重写为使用普通的、vanilla JavaScript。 This code now ships inside of Action View as rails-ujs.此代码现在作为 rails-ujs 包含在 Action View 中。

You can still use jQuery if needed, but it is no longer required by default.如果需要,您仍然可以使用 jQuery,但默认情况下不再需要它。

So, you will use rails-ujs on new applications.因此,您将在新应用程序上使用rails-ujs rails-ujs does not need jQuery . rails-ujs不需要jQuery

Adding "jquery-rails" to your gemfile allows you to use " jquery_ujs ", which is a sort of "rails compatible" library : it allows you to (eg) add a delete button / link that will delete your target just by specifiying method: delete as a parameter instead of creating a form.将“jquery-rails”添加到您的 gemfile 允许您使用“ jquery_ujs ”,它是一种“rails 兼容”库:它允许您(例如)添加一个删除按钮/链接,该按钮/链接将仅通过指定method: delete删除您的目标method: delete作为参数而不是创建表单。 ( link_to "delete", "foobar/delete", :method => 'delete' ). ( link_to "delete", "foobar/delete", :method => 'delete' )。

Other than that, there's no much difference.除此之外,没有太大区别。

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

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