我遵循这个教程:https://www.hotrails.dev/articles/rails-modals-with-hotwireEverything运行良好,直到我创建这个刺激控制器文件:
import { Controller } from "@hotwired/stimulus"
import * as bootstrap from "bootstrap"
export default class extends Controller {
connect() {
this.modal = new bootstrap.Modal(this.element)
}
open() {
if (!this.modal.isOpened) {
this.modal.show()
}
}
close(event) {
if (event.detail.success) {
this.modal.hide()
}
}
}
我收到以下错误:
"Failed to register controller: modal (controllers/modal_controller) TypeError: Failed to resolve module specifier "bootstrap". Relative references must start with either "/", "./", or "../"."个
更多信息: 我有一个gem bootstrap 在gem文件中, bootstrap css类是可访问和可用的.但Bootstrap js不可用,我不知道如何将其用于我的rails 7应用程序.
此外,我还在某个地方看到,我必须通过添加用于 bootstrap 的最后两行来更新Applation.js文件,我想请您判断一下它是否正确:
import "@hotwired/turbo-rails"
import "controllers"
import * as bootstrap from "bootstrap"
window.bootstrap = bootstrap;
如何让Bootstrap JS在我的Rails7应用程序中也可用?
谢谢
最新情况:
我还做了以下工作:
npm install bootstrap
我更新了app/javascript/application.js文件:
import "@hotwired/turbo-rails"
import "controllers"
import "bootstrap"
import * as bootstrap from "bootstrap"
window.bootstrap = bootstrap;
和importmap.rb个文件:
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 "bootstrap", to: "bootstrap/dist/js/bootstrap.bundle.min.js", preload: true
但我仍然没有将Bootstrap JS加载到我的应用程序中,并且我在控制台中仍然收到相同的错误:items:1 Uncaught TypeError: Failed to resolve module specifier "bootstrap". Relative references must start with either "/", "./", or "../".