問題摘要
我有一個新的 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:[email protected]/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
uj5u.com熱心網友回復:
我通過手動更新importmap.rb從原始bin/importmap pin jquery指向的源中固定的 jquery 源解決了這個問題:
https://ga.jspm.io/npm:[email protected]/dist/jquery.js
現在成為
https://code.jquery.com/jquery-3.6.0.min.js
此外,我添加了一些 jquery 代碼以application.js在加載檔案時運行(根據Semantic-UI 網站上的檔案
$(document).ready(function(){
$('.ui.accordion').accordion()});
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/410179.html
標籤:
