简体   繁体   English

application.js 中 require() 和 //= 的区别

[英]difference between require() and //= in application.js

In a Rails application, inside application.js, what does the lines that start with //= require mean and what is the difference with a normal require();在 Rails 应用程序中,在 application.js 中,以//= require开头的行是什么意思,与普通的require(); ? ?

I'm new at web development, at the beginning I thought this were irrelevant as they appear as commented.我是 web 开发的新手,一开始我认为这无关紧要,因为它们看起来像评论一样。

For example, this:例如,这个:

//= require cocoon
//= require jquery_nested_form
//= require jquery3
//= require jquery_ujs
//= require jquery

Rails uses Asset Pipeline(Sprokets) to bundle all js and css files in one and minifies it for better performance of the page loading. Rails 使用Asset Pipeline(Sprokets)将所有 js 和 css 文件捆绑在一起,并将其缩小以提高页面加载性能。

app/assets/javascripts/application.js called manifest file where you mention your javascript files you want to include and the order in which you want to include using app/assets/javascripts/application.js称为清单文件,您在其中提到要包含的 javascript 文件以及要包含的顺序

//= require

Similarly for css, you have app/assets/stylesheets/application.css同样对于 css,你有app/assets/stylesheets/application.css 8CBA22E28EB17B5F5C6AE2A266AZ

/* ...
*= require_self
*= require_tree .
*/

require() in context of ruby is to load other file in current file so that it's functionality can be reused. ruby 上下文中的require()是在当前文件中加载其他文件,以便可以重用它的功能。

require in context of javascritp is to load javascript modules to be used in current context/file. javascritp 上下文中的require是加载 javascript 模块以在当前上下文/文件中使用。 It's CommonJS syntaxt.它是 CommonJS 语法。 This comes into picture when you use webpacker gem.当您使用webpacker gem 时,就会出现这种情况。

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

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