简体   繁体   English

Rails 6 + Webpack document.addEventListener 不起作用

[英]Rails 6 + Webpack document.addEventListener is not Working

My code is as under我的代码如下

// app/javascript/packs/application.js
import 'select2';
document.addEventListener(
  'turbolinks:load', function() {
    jQuery('#post_category_ids').select2();
  } 
);

// app/javascript/packs/quill.js
import Quill from 'quill';
document.addEventListener(
  'turbolinks:load', function() {
    var quill = new Quill('#editor', {
      modules: {
        toolbar: true
      },
      theme: 'snow'
    });

    quill.on('text-change', function(delta, oldDelta, source) {
      $('#editorable').val(quill.container.firstChild.innerHTML);
    });
  }
);

The addEventListener in quill.js is not firing. quill.js 中的 addEventListener 没有触发。 I can get quill editor working only on hard reload.我只能让 quill 编辑器在硬重新加载时工作。 If I place all the code inside the addEvenlistener in application.js then quill loads just fine without hard refresh.如果我将所有代码放在 application.js 中的 addEvenlistener 中,那么 quill 加载就可以了,无需硬刷新。

How to solve this?如何解决这个问题?

Solved the problem.解决了问题。 I was using javascript_pack_tag helper in _form.html.erb.我在 _form.html.erb 中使用了 javascript_pack_tag 助手。 It seems 'turbolinks:load' is not available there.那里似乎没有“turbolinks:load”。 I moved the javascript_pack_tag helper to application.html.erb我将 javascript_pack_tag 助手移动到 application.html.erb

# app/views/layouts/application.html.erb
<%= javascript_pack_tag 'quill', 'data-turbolinks-track': 'reload' if action_name == "edit" %>

...and everything works as expected. ...一切都按预期进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM