简体   繁体   English

Javascript 在 Ruby on Rails 6 中编译

[英]Javascript compilation in Ruby on Rails 6

I'm using RoR 6.1.0, and would like to add a Javascript library.我正在使用 RoR 6.1.0,并想添加一个 Javascript 库。 As a test case, I'll try to add JQuery.作为测试用例,我将尝试添加 JQuery。 I am using webpacker to manage the Javascript libraries in my app.我正在使用 webpacker 来管理我的应用程序中的 Javascript 库。

First I use yarn to add jquery to the project:首先我使用yarn将jquery添加到项目中:

yarn add jquery

Next I import jquery in the webpacker application entry point, app/javascript/packs/application.js .接下来我在 webpacker 应用程序入口点app/javascript/packs/application.js中导入jquery The second line verifies the import:第二行验证导入:

import jQuery from "jquery";
console.log(jQuery.fn.jquery);

My custom javascript files are saved in app/asets/javascripts .我的自定义 javascript 文件保存在app/asets/javascripts中。 I create the file app/assets/javascripts/test.js as:我将文件app/assets/javascripts/test.js为:

import jQuery from "jquery";

function test() {
    console.log(jQuery.fn.jquery)
}

Finally, in my view, I include test.js and assign the function to a button:最后,在我看来,我包含test.js并将 function 分配给一个按钮:

<%= javascript_include_tag('test.js') %>
<button onclick="test()">Button</button>

On page load, the console displays the following error: Uncaught SyntaxError: Cannot use import statement outside a module .在页面加载时,控制台显示以下错误: Uncaught SyntaxError: Cannot use import statement outside a module I think this means the file test.js is not being processed by the ES6 compiler.我认为这意味着文件test.js没有被 ES6 编译器处理。 How can I make sure that the javascript files in app/assets/javascript are compiled properly?如何确保正确编译app/assets/javascript中的 javascript 文件?

If you are using webpacker and you want to include js files in your views/layouts then you have to use javascript_pack_tag .如果您使用webpacker并且想要在视图/布局中包含 js 文件,那么您必须使用javascript_pack_tag All these files (called entries ) should be under /packs (or where you set the source_path & source_entry_path in your webpacker.yml ).所有这些文件(称为entries )都应该在/packs下(或者您在 webpacker.yml 中设置source_pathsource_entry_pathwebpacker.yml )。 The entries should import or require different files (bundle them) then webpacker will prepare them to be browser ready (babel, loaders, transpiling, etc..)条目应该导入或需要不同的文件(捆绑它们),然后 webpacker 将准备它们以准备好浏览器(babel、loaders、transpiling 等)

Create app/javascript/packs/test.js and add these:创建app/javascript/packs/test.js并添加这些:

 require('app/assets/javascripts/test.js')

This should fix the complilation issue.这应该可以解决编译问题。 Webpacker has resolved_paths: [] in webpacaker.yml where you can add different paths to look for files. Webpacker 在webpacaker.yml中有 resolve_paths resolved_paths: [] ,您可以在其中添加不同的路径来查找文件。 You can add app/assets/javascripts and change to:您可以添加app/assets/javascripts并更改为:

require('test.js')

Now clicking that button.现在单击该按钮。 If you want to do it that way then test() should be on window .如果你想这样做,那么test()应该在window上。 Another option is to bind an onClick event for that button at document ready.另一种选择是在文档准备就绪时为该按钮绑定 onClick 事件。 There are other options but these 2 are the easy ones.还有其他选择,但这两个是简单的。

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

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