Turbo和Content_for -Rails

发布于 2025-02-07 09:06:11 字数 420 浏览 3 评论 0原文

我正在尝试仅在特定页面上加载JavaScript。为此,我正在使用<%= yart(:head)%>< head>和页面上的以下内容:

<% content_for :head do %>
  <%= javascript_import_module_tag "foo" %>
<% end %>

此功能在单击时起作用第一次指向页面的链接。但是,当我进一步浏览任何地方,然后再返回时,Turbo将头部零件(&lt; script type =“ module”&gt; import“ foo”&lt&lt;/script&gt;)。因此,此脚本不再执行。

有没有办法告诉Turbo,它不应该“缓存”?

I'm trying to load a javascript only on a certain page. For that I'm using <%= yield(:head) %> in the <head> and the following on the page:

<% content_for :head do %>
  <%= javascript_import_module_tag "foo" %>
<% end %>

This works when clicking on a link to the page the first time. However when further I navigate anywhere and then go back again, Turbo conserves the head part (<script type="module">import "foo"</script>). Thus this script is not executed anymore.

Is there a way to tell turbo, that it shouldn't 'cache' this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感性 2025-02-14 09:06:11

遇到了相同的问题。必须使用document.addeventlistener(“ turbo:load”,function(){});加载js

foo.js

document.addEventListener("turbo:load", function() {
  console.log("foo.js loaded");
  // more js codes
});

ran into same problem. had to use document.addEventListener("turbo:load", function() {} ); to load the js

foo.js

document.addEventListener("turbo:load", function() {
  console.log("foo.js loaded");
  // more js codes
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文