简体   繁体   English

如何在rails 6应用程序中使用Gem中的js文件

[英]how to use js files from Gem in rails 6 application

So I have been using rails for quite a while. 所以我一直在使用rails。 But with Rails 6 I have been really struggling to get moving. 但是对于Rails 6,我一直在努力让自己感动。 I have some custom gems I use for assets and things and I cannot figure out how to load the js files. 我有一些我用于资产和事物的自定义宝石,我无法弄清楚如何加载js文件。

What I am used to 我习惯了什么

application.js 的application.js

//= require activestorage
//= require jquery-3.3.1.min
//= require popper.min
//= require bootstrap
//= require mdb
//= require wysiwyg
//= require addons/pickr.min
//= require modules/buttons
//= require modules/cards
//= require modules/waves
//= require activestorage
//= require turbolinks
//= require_tree .

But this does not load in Rails 6 with Webpacker. 但这并没有在Webpacker的Rails 6中加载。 I was unable to find a basic solution online for this that did not involve adding multiple js files and lines of code to the app to patch a solution together. 我无法在网上找到一个基本的解决方案,不需要在应用程序中添加多个js文件和代码行来一起修补解决方案。 What I did try was 我做的尝试是

app/javascript/packs/application.js 应用程序/ JavaScript的/包/的application.js

require("@rails/ujs").start()
require("turbolinks").start()
require ("jquery-3.3.1.min").start()
require ("popper.min").start()
require ("bootstrap").start()
require ("mdb").start()
require ("wysiwyg").start()
require ("addons/pickr.min").start()
require ("modules/buttons").start()
require ("modules/cards").start()
require ("modules/waves").start()
require("@rails/activestorage").start()
require("channels")

The assets are in the correct place inside the gem (so the first version in a rails 5 app does load everything as expected). 资产位于gem内部的正确位置(因此rails 5应用程序中的第一个版本会按预期加载所有内容)。 I can add some of these with yarn, but I want to use the actual files from the gem, not just get bootstrap working, is there a straightforward solution to this? 我可以添加一些这些与纱线,但我想使用来自gem的实际文件,而不仅仅是让bootstrap工作,是否有一个简单的解决方案? I also tried adjusting the path in the require but that did not work either. 我也尝试调整require的路径但是也没有用。

Thanks for any help! 谢谢你的帮助!

You need to import js files via gem. 您需要通过gem导入js文件。 Maybe this can help you: 也许这可以帮助你:

import jQuery from 'jquery'

<%= File.read(File.join(Gem.loaded_specs['bla_bla_gem'].full_gem_path, 'lib', 'assets', 'javascripts', 'bla_bla.js')) %>

Related issue comment 相关问题评论

So to answer in rails 6 you will notice the use of the javascript folder app/javascript this means that anything you do with JS should be done from there. 所以要在rails 6中回答你会注意到使用javascript文件夹app / javascript这意味着你用JS做的任何事都应该从那里完成。 The reliance on gems for these is now minimal and replaced by yarn. 对宝石的依赖现在很小,取而代之的是纱线。

I will give you an example of my process for using js libraries. 我将举例说明我使用js库的过程。 for the example i will call bootstrap using yarn. 例如,我将使用纱线调用bootstrap。 I have included the custom files to help you call each library. 我已经包含了自定义文件来帮助您调用每个库。

this is a process I like to use which I have modified from something i read on medium a while back 这是我喜欢使用的一个过程,我已经修改了一段时间后我在媒体上阅读的内容

# app/javascript/packs/application.js
import '../stylesheets/application'


# app/views/layouts/application.html.erb
<%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
in the console run

yarn add bootstrap@4.3.1 jquery popper.js

then 然后

# config/webpack/environment.js
...
const webpack = require('webpack')
environment.plugins.append(
  'Provide',
  new webpack.ProvidePlugin({
    $: 'jquery',
    jQuery: 'jquery',
    Popper: ['popper.js', 'default']
  })
)

... then ... 然后

# app/javascript/packs/bootstrap_custom.js
import 'bootstrap/js/dist/alert'
import 'bootstrap/js/dist/button'
import 'bootstrap/js/dist/carousel'
import 'bootstrap/js/dist/collapse'
import 'bootstrap/js/dist/dropdown'
import 'bootstrap/js/dist/index'
import 'bootstrap/js/dist/modal'
import 'bootstrap/js/dist/popover'
import 'bootstrap/js/dist/scrollspy'
import 'bootstrap/js/dist/tab'
import 'bootstrap/js/dist/toast'
import 'bootstrap/js/dist/tooltip'
import 'bootstrap/js/dist/util'

And link it in your app/javascript/packs/application.js file. 并将其链接到您的app / javascript / packs / application.js文件中。

# app/javascript/packs/application.js
import './bootstrap_custom.js'
then

# app/javascript/stylesheets/application.scss
@import './bootstrap_custom.scss'
then

# app/javascript/stylesheets/bootstrap_custom.scss
@import '~bootstrap/scss/_functions.scss';
@import '~bootstrap/scss/_variables.scss';
@import '~bootstrap/scss/_mixins.scss';
@import '~bootstrap/scss/_root.scss';
@import '~bootstrap/scss/_reboot.scss';
@import '~bootstrap/scss/_type.scss';
@import '~bootstrap/scss/_alert.scss';
@import '~bootstrap/scss/_badge';
@import '~bootstrap/scss/_breadcrumb';
@import '~bootstrap/scss/_button-group';
@import '~bootstrap/scss/_buttons';
@import '~bootstrap/scss/_buttons.scss';
@import '~bootstrap/scss/_card.scss';
@import '~bootstrap/scss/_carousel.scss';
@import '~bootstrap/scss/_close.scss';
@import '~bootstrap/scss/_code.scss';
@import '~bootstrap/scss/_custom-forms.scss';
@import '~bootstrap/scss/_dropdown.scss';
@import '~bootstrap/scss/_forms.scss';
@import '~bootstrap/scss/_grid.scss';
@import '~bootstrap/scss/_images.scss';
@import '~bootstrap/scss/_input-group.scss';
@import '~bootstrap/scss/_jumbotron.scss';
@import '~bootstrap/scss/_list-group.scss';
@import '~bootstrap/scss/_media.scss';
@import '~bootstrap/scss/_modal.scss';
@import '~bootstrap/scss/_nav.scss';
@import '~bootstrap/scss/_navbar.scss';
@import '~bootstrap/scss/_pagination.scss';
@import '~bootstrap/scss/_popover.scss';
@import '~bootstrap/scss/_print.scss';
@import '~bootstrap/scss/_progress.scss';
@import '~bootstrap/scss/_spinners.scss';
@import '~bootstrap/scss/_tables.scss';
@import '~bootstrap/scss/_toasts.scss';
@import '~bootstrap/scss/_tooltip.scss';
@import '~bootstrap/scss/_transitions.scss';
@import '~bootstrap/scss/_utilities.scss';

If you choose to follow this follow it precisely, do not change the scss lines etc it will mess it up 如果你选择遵循这一点,请严格遵循它,不要改变scss线等会弄乱它

You'll need to get them directly from the gem files. 您需要直接从gem文件中获取它们。

Change your application.js to application.js.erb and follow demir solution. 将您的application.js更改为application.js.erb并遵循demir解决方案。 You can also use the next to import more than one file. 您也可以使用下一个导入多个文件。

<% ['file_1', 'file_2'].each do |file| %>
 import "<%= File.join(Gem.loaded_specs['my_gem'].full_gem_path, 'app', 'assets', 'javascripts', file) %>";
<% end %>

I think the location of file may be creating an issue for your case: File should be in app/assets/javascripts/application.js while the your file is in app/javascript/packs/application.js 我认为文件的位置可能会为您的案例创建一个问题:文件应位于app/assets/javascripts/application.js而您的文件位于app/javascript/packs/application.js

Hope this will help you. 希望这会帮助你。

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

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