简体   繁体   English

Rails 6 - Javascript:未捕获的 ReferenceError:未定义 Quill

[英]Rails 6 - Javascript: Uncaught ReferenceError: Quill is not defined

I am trying to set up Quill on Rails 6 app.我正在尝试在 Rails 6 应用程序上设置 Quill。 When I put js files in html it works, it looks like these: new.html.erb当我将 js 文件放入 html 中时,它可以工作,它看起来像这样: new.html.erb

<div id="editor">
  <p>Hello World!</p>
</div>

<script src="https://cdn.quilljs.com/1.3.6/quill.js"></script>

<!-- Initialize Quill editor -->
<script>
  var quill = new Quill('#editor', {
    theme: 'snow'
  });
</script>

However, when I try to run local js files, it gives me an error但是,当我尝试运行本地 js 文件时,它给了我一个错误

Uncaught ReferenceError: Quill is not defined
    at Object../app/javascript/src/editor.js (editor.js:1)

Here is my application.js file这是我的 application.js 文件

require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
require("src/quill.js")
require("src/editor.js")

Here is my editor.js file这是我的 editor.js 文件

var quill = new Quill('#editor', {
      theme: 'snow'
    });

and quilljs is the whole library file. quilljs 是整个库文件。

my application.html.erb has this tag as well我的 application.html.erb 也有这个标签

    <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>

Both js files do load, because I can see the console.log value that I set up in console.两个 js 文件都会加载,因为我可以看到我在控制台中设置的 console.log 值。 I am not sure what I am doing wrong and it seems like no one had similar problems.我不确定我做错了什么,似乎没有人有类似的问题。

Do your asset.rb have it?你的asset.rb 有吗?

Rails.application.config.assets.precompile += %w( application.js )

You probably just to have to wait for the DOM to load before creating an instance of Quill.在创建 Quill 实例之前,您可能只需要等待 DOM 加载。 This question was asked last year, so hopefully you've found a solution already.这个问题是去年提出的,所以希望你已经找到了解决方案。 If not, you could try something like this:如果没有,你可以尝试这样的事情:

document.addEventListener("DOMContentLoaded", function (event) {
  var quill = new Quill('#editor', {
    theme: 'snow'
  });
});

In case you or anyone else is interested, I wrote a blog post about my experience adding QuillJS to Rails 6 recently which could be of use.如果您或其他任何人感兴趣,我写了一篇关于我最近将QuillJS 添加到 Rails 6的经验的博客文章,这可能会有用。 The post admittedly doesn't go into detail about this particular problem but the code snippets do solve it and could provide more help with configuration more generally.诚然,这篇文章并没有 go 详细介绍这个特定问题,但代码片段确实解决了这个问题,并且可以为更一般的配置提供更多帮助。

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

相关问题 JavaScript和Rails中未定义未捕获的ReferenceError功能 - Uncaught ReferenceError Fucntion is not defined in JavaScript and Rails Ruby on Rails - 未捕获的 ReferenceError:$ 未定义 - Ruby on Rails - Uncaught ReferenceError: $ is not defined Rails 6 - 未捕获的 ReferenceError:未定义 MStepper - Rails 6 - Uncaught ReferenceError: MStepper is not defined Rails Javascript onsubmit validate()未捕获ReferenceError:未定义validate - Rails Javascript onsubmit validate() Uncaught ReferenceError: validate is not defined Ruby on Rails 6 自定义 JavaScript 文件 Uncaught ReferenceError: Rellax is not defined - Ruby on Rails 6 custom JavaScript file Uncaught ReferenceError: Rellax is not defined Javascript。 未捕获的ReferenceError:未定义OnChange - Javascript . Uncaught ReferenceError: OnChange is not defined 未捕获的ReferenceError:未定义“更改”(JavaScript) - Uncaught ReferenceError: “changing” is not defined (JavaScript) Javascript-未捕获的ReferenceError:函数未定义 - Javascript - Uncaught ReferenceError: function is not defined javascript Uncaught ReferenceError:未定义uneval - javascript Uncaught ReferenceError: uneval is not defined Javascript Uncaught ReferenceError:函数未定义 - Javascript Uncaught ReferenceError: Function is not defined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM