简体   繁体   English

Rails使用清单文件在不同页面中加载不同的javascript文件

[英]Rails load different javascript files in different pages using manifest file

I am using different javascript libraries in different part of my rails app. 我在Rails应用程序的不同部分中使用了不同的JavaScript库。 Currently all my library files are in application.js manifest file. 目前,我所有的库文件都在application.js清单文件中。 And it gets loaded in pages, where it's not required. 它被加载到不需要的页面中。 Is there any way I can load files that are needed in a particular part of my app? 有什么方法可以加载应用程序特定部分所需的文件?

You can add a separate "manifest" and load that on demand where you need it. 您可以添加单独的“清单”,然后根据需要将其加载到需要的地方。 You will have to: 你不得不:

1- Tell Rails to preprocess those files, ie if your file is named additional_dependencies.js , then add it in assets.rb : 1 -告诉Rails预处理这些文件,也就是说,如果你的文件被命名为additional_dependencies.js ,然后将其添加在assets.rb

Rails.application.config.assets.precompile += %w( additional_dependencies.js )

2- Load that file where you need it (NOT in the main layout): 2-将文件加载到需要的位置(不在主布局中):

= javascript_include_tag 'additional_dependencies'

And, of course, do not add the files you want to keep as separate dependencies into the application.js file (watch for require_tree). 而且,当然不要将要保留的文件作为独立的依赖项添加到application.js文件中(注意require_tree)。

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

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