[英]Should I still use Gemfile's javascript library when using yarn with Rails 5.1?
I'm trying to get some jQuery stuff in Rails 5.1. 我试图在Rails 5.1中获得一些jQuery的东西。 Since the jQuery support is dropped (by default) I'm getting some hard time to find the exact workflow I should use. 由于jQuery支持被删除(默认情况下),我很难找到我应该使用的确切工作流程。
At first I would try some Ajax stuffs but then I realized in the web developer console that '$' was not defined. 起初我会尝试一些Ajax的东西,但后来我在Web开发者控制台中意识到'$'没有定义。 This confirming that jQuery is indeed not installed by default. 这确认了默认情况下确实没有安装jQuery。
1. The first thing I did is simply put a hard coded jquery cdn script in the application layout view, something like this in app/views/layouts/application.html.erb
: 1.我做的第一件事就是在应用程序布局视图中放置一个硬编码的jquery cdn脚本,就像在app/views/layouts/application.html.erb
:
<script src="https://code.jquery.com/jquery-3.2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous"></script>
This worked. 这很有效。 But I guess that this is not the supported way of doing it. 但我想这不是支持的方式。
2. Then I tried with installing jquery using yarn, since I have a Rails 5.1 installation with yarn already installed. 2.然后我尝试使用纱线安装jquery,因为我已经安装了纱线的Rails 5.1安装。
yarn add jquery
then yarn
(seems this last is not necessary) 我跑yarn add jquery
然后yarn
(似乎这最后没有必要) //= require jquery
to the app/assets/javascripts/application.js
file 我在app/assets/javascripts/application.js
文件中添加了//= require jquery
This worked too. 这也有效。
Edit: I just noticed that there is a rails yarn:install
command, should I use that instead of the terminal native yarn command? 编辑:我刚注意到有一个rails yarn:install
命令,我应该使用它而不是终端本机yarn命令吗?
But then I wanted to play with some jQuery-ui stuffs. 但后来我想玩一些jQuery-ui的东西。 Noticing that the package jquery-ui
exists in yarn, I did the same thing: 注意到包装jquery-ui
存在于纱线中,我做了同样的事情:
yarn add jquery-ui
then yarn
again 我再用yarn
yarn add jquery-ui
然后yarn
//= require jquery-ui
to the app/assets/javascripts/application.js
file right below the jquery
one 我在jquery
下面的app/assets/javascripts/application.js
文件中添加了//= require jquery-ui
But this did not work. 但这没效果。 Then I noticed that some people refer to jquery gems, particularly the gems jquery-rails
and jquery-ui-rails
. 然后我注意到有些人引用了jquery gems,特别是gems jquery-rails
和jquery-ui-rails
。
My question is despite having a working yarn pipeline , should I still use the related gems? 我的问题是,尽管有一条工作的纱线管道 ,我还应该使用相关的宝石吗?
In my case should I only use these both gems? 在我的情况下,我应该只使用这两个宝石吗? Or should I only install the jquery-ui-rails
one since my jquery installation with yarn is working? 或者我应该只安装jquery-ui-rails
因为我的jquery安装纱线正在工作?
I'm sorry but this asset management thing is not yet get into my head.. And still not yet starting webpack stuffs 😖 对不起,但这个资产管理的事情还没有进入我的脑海......还没有启动webpack的东西😖
I made it work without Rails gems and with yarn packages. 我没有Rails宝石和纱线包装就可以了。
Instead of this 而不是这个
Gemfile: 的Gemfile:
gem 'jquery-rails'
gem 'jquery-ui-rails'
Solution. 解。
add yarn packages in package.json 在package.json中添加yarn包
{
...
"dependencies": {
"jquery": "3.2.1",
"jquery-ui-dist": "1.12.1",
"jquery-ujs": "1.2.2",
.. more packages here
}
}
Note! 注意! it uses package jquery-ui-dist (not jquery-ui). 它使用包jquery-ui-dist(不是jquery-ui)。
application.js: application.js中:
//= require jquery/dist/jquery
//= require jquery-ujs
//= require jquery-ui-dist/jquery-ui
About jquery-ui packages: the jQuery-ui package on NPM isn't pre-built and therefore doesn't contain jquery-ui.js; 关于jquery-ui包:NPM上的jQuery-ui包不是预构建的,因此不包含jquery-ui.js; the package will either need built before use or all the widgets included/used individually. 包将需要在使用之前构建或者包含/单独使用的所有小部件。 Read more in this discussion - jquery-ui and webpack, how to manage it into module? 在这个讨论中阅读更多内容 - jquery-ui和webpack,如何将其管理到模块中? . 。
您可以查看项目根目录中的node_modules
文件夹 - 在那里您将找到所有安装了YARN软件包的文件夹,并且应该有您添加到YARN并想要使用的库的文件夹。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.