0

问题摘要

我有一个新的 rails 7 应用程序,它使用importmap-railsgem 来管理 javascript 前端库。

我正在尝试使用 Semantic-UI 库,但是 Semantic-UI javascript 无法识别 jQuery 的依赖关系。

我无法通过 importmap 固定 Semantic-UI 库,所以我importmap.rb手动添加了这个库。这是我正在使用的语义.min.js 的 CDN 链接

以下错误消息出现在 Chrome Web 工具的控制台中,并且我在页面上验证 javascript 的可折叠手风琴不起作用。

Uncaught ReferenceError: jQuery is not defined
    at semantic.min.js:11:6043
(anonymous) @ semantic.min.js:11

中引用的行semantic.min.js如下所示

(jQuery,window,document)

尝试修复

我认为 Semantic-UI 和 jQuery 导入发生的顺序可能是问题(根据许多其他Stack Overflow 问题Github 问题)。

但是,我在 Semantic-UI 导入之前进行了 jQuery 导入。

如果我注释掉 Semantic-UI 导入,我可以毫无问题地使用 jQuery。

我已经切换了文件preload上的变量,importmap.rb 以查看 Semantic-UI 是否也被加载得太快。

我尝试将 jQuery 导入重命名为$and jQuery,但最终都没有被识别。

我的直觉说 Semantic-UI 在 jQuery 设置为全局变量之前加载,但我不确定它如何与新的 importmap 功能和资产管道一起工作。

应用程序配置

导入地图.rb

# Pin npm packages by running ./bin/importmap

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 "jquery", to: "https://ga.jspm.io/npm:jquery@3.6.0/dist/jquery.js", preload: true
pin "semantic-ui", to: "https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.js"

pin_all_from "app/javascript/controllers", under: "controllers"

应用程序.js

// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
import "@hotwired/turbo-rails"
import "controllers"
import "jquery" 
import "semantic-ui"

index.html.erb 可折叠手风琴

<div class="ui styled accordion">
  <div class="title">
    <i class="dropdown icon"></i>
    What is a dog?
  </div>
  <div class="content">
    <p class="transition hidden">A dog is a type of domesticated animal. Known for its loyalty and faithfulness, it can be found as a welcome guest in many households across the world.</p>
  </div>
  <div class="title active">
    <i class="dropdown icon"></i>
    What kinds of dogs are there?
  </div>
  <div class="content active">
    <p class="transition visible">There are many breeds of dogs. Each breed varies in size and temperament. Owners often select a breed of dog that they find to be compatible with their own lifestyle and desires from a companion.</p>
  </div>
  <div class="title">
    <i class="dropdown icon"></i>
    How do you acquire a dog?
  </div>
  <div class="content">
    <p>Three common ways for a prospective owner to acquire a dog is from pet shops, private owners, or shelters.</p>
    <p>A pet shop may be the most convenient way to buy a dog. Buying a dog from a private owner allows you to assess the pedigree and upbringing of your dog before choosing to take it home. Lastly, finding your dog from a shelter, helps give a good home to a dog who may not find one so readily.</p>
  </div>
</div>

宝石文件

source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby "3.0.1"

# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "~> 7.0.0"

# The original asset pipeline for Rails [https://github.com/rails/sprockets-rails]
gem "sprockets-rails"
gem "semantic-ui-sass"

# Use postgresql as the database for Active Record
gem "pg", "~> 1.1"

# Use the Puma web server [https://github.com/puma/puma]
gem "puma", "~> 5.0"

# Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails]
gem "importmap-rails"

# Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev]
gem "turbo-rails"

# Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev]
gem "stimulus-rails"

# Build JSON APIs with ease [https://github.com/rails/jbuilder]
gem "jbuilder"

# Use Redis adapter to run Action Cable in production
# gem "redis", "~> 4.0"

# Use Kredis to get higher-level data types in Redis [https://github.com/rails/kredis]
# gem "kredis"

# Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword]
# gem "bcrypt", "~> 3.1.7"

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]

# Reduces boot times through caching; required in config/boot.rb
gem "bootsnap", require: false

# Use Sass to process CSS
# gem "sassc-rails"

# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"

group :development, :test do
  # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
  gem "debug", platforms: %i[ mri mingw x64_mingw ]
end

group :development do
  # Use console on exceptions pages [https://github.com/rails/web-console]
  gem "web-console"

  # Add speed badges [https://github.com/MiniProfiler/rack-mini-profiler]
  # gem "rack-mini-profiler"

  # Speed up commands on slow machines / big apps [https://github.com/rails/spring]
  # gem "spring"
end

group :test do
  # Use system testing [https://guides.rubyonrails.org/testing.html#system-testing]
  gem "capybara"
  gem "selenium-webdriver"
  gem "webdrivers"
end

4

1 回答 1

2

我通过手动更新importmap.rb从原始bin/importmap pin jquery指向的源中固定的 jquery 源解决了这个问题:

https://ga.jspm.io/npm:jquery@3.6.0/dist/jquery.js

现在成为

https://code.jquery.com/jquery-3.6.0.min.js

此外,我添加了一些 jquery 代码以application.js在加载文档时运行(根据Semantic-UI 网站上的文档

$(document).ready(function(){
  $('.ui.accordion').accordion()});
于 2022-01-10T19:11:52.047 回答