我希望在Rails应用程序中有一个可用于视图的JavaScript文件。
// app/javascript/custom/manage.js
document.body.style.backgroundColor = 'lightblue'
const test = document.getElementById('test')
# config/importmap.rb
pin_all_from "app/javascript/custom", under: "custom"
# app/views/locations/manage.html.erb
<%= javascript_include_tag "custom/manage", defer: true %>
当我回到manage
视图时,我得到以下错误:
Uncaught SyntaxError: redeclaration of const test
我怀疑这是因为Turbo的工作方式。因为我尝试在application.js
文件中注解它,错误消失了。
1条答案
按热度按时间flmtquvp1#
据我所知,你需要选择性导入模块功能
可以选择性地导入特定页面上的模块,使用
content_for
在视图顶部添加在布局中
javascript_importmap_tags
应该是第一个