简体   繁体   中英

Rails load different javascript files in different pages using manifest file

I am using different javascript libraries in different part of my rails app. Currently all my library files are in application.js manifest file. 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 :

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

2- Load that file where you need it (NOT in the main layout):

= 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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