$(document).on('turbolinks:load',function(){不起作用

时间:2019-12-03 23:38:39

标签: javascript ruby-on-rails turbolinks ruby-on-rails-6

我正在尝试准备好我的Turbolinks文档。我的代码很简单

$( document ).on('turbolinks:load', function() {
  console.log("It works on each visit!")
})

这是使用rails 6的。该代码位于我的application.js文件的中间:

// This file is automatically compiled by Webpack, along with any other files
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.
// Loads all Semantic javascripts
//= require jquery
//= require semantic-ui
//= require rails-ujs
//= require activestorage
//= require turbolinks
//= require_tree .
// Uncomment to copy all static images under ../images to the output folder and reference
// them with the image_pack_tag helper in views (e.g <%= image_pack_tag 'rails.png' %>)
// or the `imagePath` JavaScript helper below.
//
// const images = require.context('../images', true)
// const imagePath = (name) => images(name, true)

$(document).on('turbolinks:load', function() {
$('.message .close')
  .on('click', function() {
    $(this).closest('.message').transition('fade');
  })
;
})

$( document ).on('turbolinks:load', function() {
  console.log("It works on each visit!")
})

我的gemfile中同时具有jquery和turbolinks gem,并且已安装包。当前没有显示控制台日志,但是也没有错误消息。

1 个答案:

答案 0 :(得分:1)

问题是您的mainfest中没有require_self指令。

// This file is automatically compiled by Webpack, along with any other files
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.
// Loads all Semantic javascripts
//= require jquery
//= require semantic-ui
//= require rails-ujs
//= require activestorage
//= require turbolinks
//= require_self
//= require_tree .

其单独的指令的原因是它使您可以影响文件的编译顺序。