Rails 5 应用程序不会从 assets/javascripts 文件夹加载 JS



,据我了解,如果application.js文件中存在 /= require_tree,则加载了资产/javascripts文件夹中的任何JS文件。但是,我无法让我的JS代码工作。我把它放在assets/javascripts/locations.js中,它不起作用。当我将其放入script文件中的CC_3标签中时,另一方面可以完美地工作。有什么想法我可能会想念什么?跑步5.2。JS代码在下面。

locations.js

var locations = ["coordinatesStore"]
  .map(id => document.getElementById(id));
Array.prototype.forEach.call(
  document.querySelectorAll('.add-button'),
  (button, i) => {
    button.addEventListener('click', () => getLocation(i));
  }
);
function getLocation(i) {
  var location = locations[i];
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(({ coords: { latitude, longitude }}) => {
      location.value = latitude + ", " + longitude;
    });
  } else { 
    location.value = "Geolocation is not supported by this browser.";
  }
}

我在我的application.html.erb

中有此标签
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>

在哪里 <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>在您的应用程序中。html.erb?

尝试将其放在身体标签的末端,至少在<%= yield %>

之后

最新更新