简体   繁体   English

Uncaught ReferenceError: Clipboard is not defined: when using clipboard.js with Rails 6

[英]Uncaught ReferenceError: Clipboard is not defined: when using clipboard.js with Rails 6

I'm trying to use 'clipboard.js' in my Rails 6 application to copy the current location to the clipboard for the user to paste into an email.我试图在我的 Rails 6 应用程序中使用“clipboard.js”将当前位置复制到剪贴板,以便用户粘贴到 email。 My first attempt was to use 'clipboard-rails' but that didn't work so have tried manually.我的第一次尝试是使用“剪贴板导轨”,但没有奏效,因此手动尝试。

app/views/layouts/application.html.erb:-
----------------------------------------
<%= javascript_pack_tag 'clipboard', 'data-turbolinks-track': 'reload' %>



app/javascript/packs/application.js:-
-------------------------------------
require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
require("packs/clipboard")

var clipboard = new Clipboard('.clipboard-btn');

clipboard.on('success', function(e) {
    console.log(e);
});

clipboard.on('error', function(e) {
    console.log(e);
});


import "bootstrap"




app/views/bundles/show.html.erb:-
---------------------------------
<p class="link" id="bar"><%= "#{request.protocol}#{request.host_with_port}#{request.fullpath}" %></p>
<button class="clipboard-btn" data-clipboard-action="copy" data-clipboard-target="#bar">
    Copy to clipboard
</button>

I keep getting 'Uncaught ReferenceError: Clipboard is not defined' error in the browser console and pressing the button does nothing.我在浏览器控制台中不断收到“未捕获的引用错误:剪贴板未定义”错误,按下按钮什么也不做。 I've tried 'Clipboard' and 'ClipboardJS'.我试过“剪贴板”和“剪贴板JS”。 I'm developing on Windows 10.我正在 Windows 10 上开发。

You probably have to assign the require to Clipboard:您可能必须将要求分配给剪贴板:

    const Clipboard = require("packs/clipboard");
    var clipboard = new Clipboard('.clipboard-btn');

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

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