【发布时间】:2022-05-06 00:32:42
【问题描述】:
我正在尝试在一个新的 Rails 7 应用程序上使用 Select2,但遇到以下问题:
我已将它固定到我的导入地图中并像这样导入它:
pin "application", preload: true
pin "@hotwired/turbo-rails", to: "turbo.min.js", preload: true
pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true
pin_all_from "app/javascript/controllers", under: "controllers"
pin "trix"
pin "@rails/actiontext", to: "actiontext.js"
pin "select2", to: "https://ga.jspm.io/npm:select2@4.1.0-rc.0/dist/js/select2.js"
pin "jquery", to: "https://ga.jspm.io/npm:jquery@3.6.0/dist/jquery.js"
(最后两行是我运行 bin/importmap pin select2 时添加的)
import "jquery";
import "select2";
import "@hotwired/turbo-rails";
import "controllers";
import "trix";
import "@rails/actiontext";
(已将 jquery 和 select2 移到末尾和开头 - 没有改变任何事情)。
当我在表单中时,我可以像这样使用 $ 访问元素:
$('#book_genre_ids');
...(returns the element)
但是当我手动尝试 - 在控制台中 - 在元素上运行 select2() 时,会发生以下情况:
$('#book_genre_ids').select2();
VM574:1 Uncaught TypeError: $(...).select2 is not a function
at <anonymous>:1:22
我确实检查了网络源(chrome 控制台),我可以从 gap.jspm.io 找到 npm:jquery@3.6.0/dist 和 npm:select2@4.1.0-rc.0/dist。 我发现了一些指向正在加载的多个 jquery 库的资源,但是我在控制台的网络源中没有找到比上述更多的资源...
【问题讨论】:
标签: ruby-on-rails jquery-select2 ruby-on-rails-7 import-maps