javascript - 使用 Bootstrap 5.0.0 和 Webpacker 的动态模式
问题描述
从 Bootstrap 5.0.0 开始,似乎 Bootstrap JS 文件必须在最后加载<body>
才能呈现动态加载的 Modals(可能还有其他功能)。在 GitHub 上查看此问题
同意这个问题。这发生在 bootstrap 5.x 生产版本中,无论您如何初始化模态。通过 JS 或通过 Data 属性动态地。
目前解决此问题的解决方法是
bootstrap.bundle.js
在 HTML 正文中而不是在头部中加载。但理想情况下,它不能解决这个问题。
你知道这个问题的另一种解决方法吗?
错误:
// Error happens when I want to display a Modal using JS
var modal = new bootstrap.Modal(document.getElementById('my-modal');
modal.show();
// Uncaught TypeError: BACKDROP: Option "rootElement" provided type "null" but expected type "element".
// typeCheckConfig bootstrap.bundle.js:190
// typeCheckConfig bootstrap.bundle.js:184
// _getConfig bootstrap.bundle.js:4603
// Backdrop bootstrap.bundle.js:4539
// _initializeBackDrop bootstrap.bundle.js:4864
// Modal bootstrap.bundle.js:4721
// <anonymous> debugger eval code:2
一个解法:
<body>
最简单的解决方法是在标签末尾加载我们的 JS :
# app/views/layout/application.html.erb
<body>
<%= yield %>
<%# this `javascript_pack_tag` is initially inside the `<head>` tag
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
</body>
解决方案
推荐阅读
- java - scheduleAtFixRate 每 10 分钟运行一次,而不是每小时运行一次
- spring-boot - 为 spring-security-taglibs 添加依赖会抛出由 org.springframework.beans.factory.BeanCreationException 引起的 javax.servlet.ServletException
- android - RecyclerView - 使用键盘过滤项目,如 ListView
- c# - WPF Treeview从数据库中添加子项目
- powershell - Powershell AD 自动创建访客用户
- php - Laravel,如何将许多数组与来自foreach的数组本身合并?
- java - 连接到 Cloud SQL (Mysql) 时,Google Cloud 功能出现间歇性异常
- sql-server - 使用 SQL Server 触发器发送电子邮件(根据客户端,收件人电子邮件地址是动态的)
- python - 使用 Python 运行没有文件扩展名的可执行文件
- vue.js - 为什么控制台显示错误:激活器插槽必须绑定,当我已经绑定它时?