我阅读了如何将自定义 JS 文件添加到新的 Rails 7 项目并且我写了Rails 7:如何添加自定义 javascript 函数? 我想现在我已经清楚了。
我不想加载模块。我只想加载一个带有一些像这样的自定义函数的 JavaScript 文件。
function btn_project_show() {
alert('culodemono');
}
使用此代码...(以及上面帖子中提到的所有其他内容)
<%= javascript_importmap_tags %>
rails 创建此代码...(我知道 importmap 意味着源是一个 javascript 模块,而不是具有简单功能的简单文件)
<!DOCTYPE html>
<html>
<head>
<title>Custom Admin</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="csrf-param" content="authenticity_token" />
<meta name="csrf-token" content="J96RvzdbOjB5WdfZtE0-eFIoJpISdZP3N32YPIiotPmIsTBlGyL69NlqShdPCadKOv4ReV2PFoGdMZ9CH1kxdw" />
<link rel="stylesheet" href="/assets/application-ed7f657c82113ab3654e0f1c0a9fb78eb9439717ba426d64a05914a481e366bf.css" data-turbo-track="reload" />
<link rel="stylesheet" href="/assets/vendor-342cc5ede18e0d08cf0a816961342ad3262763c3e440dbabc6bca4f058bbf174.css" data-turbo-track="reload" />
<script type="importmap" data-turbo-track="reload">{
"imports": {
"application": "/assets/application-37f365cbecf1fa2810a8303f4b6571676fa1f9c56c248528bc14ddb857531b95.js",
"@hotwired/turbo-rails": "/assets/turbo.min-dfd93b3092d1d0ff56557294538d069bdbb28977d3987cb39bc0dd892f32fc57.js",
"@hotwired/stimulus": "/assets/stimulus.min-dd364f16ec9504dfb72672295637a1c8838773b01c0b441bd41008124c407894.js",
"@hotwired/stimulus-loading": "/assets/stimulus-loading-3576ce92b149ad5d6959438c6f291e2426c86df3b874c525b30faad51b0d96b3.js",
"controllers/application": "/assets/controllers/application-368d98631bccbf2349e0d4f8269afb3fe9625118341966de054759d96ea86c7e.js",
"controllers/hello_controller": "/assets/controllers/hello_controller-549135e8e7c683a538c3d6d517339ba470fcfb79d62f738a0a089ba41851a554.js",
"controllers": "/assets/controllers/index-2db729dddcc5b979110e98de4b6720f83f91a123172e87281d5a58410fc43806.js",
"custom": "/assets/custom/index-fd29fe543537735de09c23dfa7241cdd8e47c8f9fd71e89211b591fc7c12fd8c.js""
}
}</script>
<link rel="modulepreload" href="/assets/application-37f365cbecf1fa2810a8303f4b6571676fa1f9c56c248528bc14ddb857531b95.js">
<link rel="modulepreload" href="/assets/turbo.min-dfd93b3092d1d0ff56557294538d069bdbb28977d3987cb39bc0dd892f32fc57.js">
<link rel="modulepreload" href="/assets/stimulus.min-dd364f16ec9504dfb72672295637a1c8838773b01c0b441bd41008124c407894.js">
<link rel="modulepreload" href="/assets/stimulus-loading-3576ce92b149ad5d6959438c6f291e2426c86df3b874c525b30faad51b0d96b3.js">
<script src="/assets/es-module-shims.min-4ca9b3dd5e434131e3bb4b0c1d7dff3bfd4035672a5086deec6f73979a49be73.js" async="async" data-turbo-track="reload"></script>
<script type="module">import "application"</script>
但我只想要这种链接(但由资产管道使用rails asset:precompile创建)
<script src="/assets/fluzo/index-fd29fe543537735de09c23dfa7241cdd8e47c8f9fd71e89211b591fc7c12fd8c.js"></script>
否则,我的简单javascript函数找不到。
将其添加到您的布局中:
<%= javascript_include_tag "custom/index" %>
不要将该文件导入到其他任何地方,并从 importmap.rb 中删除它的
pin
。
请记住,您仍然可以使用模块创建函数,只需将它们设为全局即可在模块外部使用:
window.btn_project_show = function() {
alert('culodemono');
}