ruby-on-rails - Rails 6 + webpack 错误:未捕获的 TypeError:$(...).popover 不是函数
问题描述
我正在 Rails 6 中开始一个新项目,这将是我第一次使用 Webpack,我在使用 popover 、 tooltip 、 dropdown时遇到问题。
application.js:37 Uncaught TypeError: $(...).dropdown 不是函数
环境.js
const { environment } = require('@rails/webpacker')
const webpack = require('webpack')
environment.plugins.append('Provide',
new webpack.ProvidePlugin({
$: 'jquery/src/jquery',
jQuery: 'jquery/src/jquery',
jquery: 'jquery',
'window.jQuery': 'jquery',
Popper: ['popper.js', 'default'],
Rails: ['@rails/ujs']
})
)
module.exports = environment
应用程序.js
import 'core-js/stable'
import 'regenerator-runtime/runtime'
require("@rails/ujs").start()
require("turbolinks").start()
require("channels")
import 'bootstrap'
import './menu-left.cleanui-custom'
import './menu-right.cleanui-custom'
// Uncomment to copy all static images under ../images to the output folder and reference
// them with the image_pack_tag helper in views (e.g <%= image_pack_tag 'rails.png' %>)
// or the `imagePath` JavaScript helper below.
//
// const images = require.context('../images', true)
// const imagePath = (name) => images(name, true)
import '../stylesheets/application'
document.addEventListener("turbolinks:load", () => {
$('[data-toggle=popover-hover]').popover({
trigger: 'hover',
animation: false
});
$("[data-toggle=tooltip]").tooltip({
trigger: 'hover',
animation: false
});
});
$('.dropdown-toggle').dropdown();
错误可能来自哪里?事实是我仍然对 webpack 很迷茫。
解决方案
这对我有用:
config/webpack/environment.js
const { environment } = require('@rails/webpacker')
const webpack = require('webpack')
environment.plugins.prepend('Provide',
new webpack.ProvidePlugin({
$: 'jquery/src/jquery',
jQuery: 'jquery/src/jquery',
Popper: ['popper.js', 'default']
})
)
environment.config.merge(
module.exports = {
resolve: {
alias: {
jquery: 'jquery/src/jquery',
}
}
}
)
module.exports = environment
app/javascript/packs/application.js
require("@rails/ujs").start()
require("turbolinks").start()
import "jquery"
import 'bootstrap'
document.addEventListener("turbolinks:load", function() {
$(function () {
$('[data-toggle="tooltip"]').tooltip()
$('[data-toggle="popover"]').popover()
})
})
推荐阅读
- javascript - 为什么这个 if 语句有效,而这个其他解决方案却没有?
- sas - 试图从数据中排除程序代码
- php - php 格式字符串以删除第一个元素
- java - 如何使用 sqlite db 编译 java 控制台应用程序(应用程序仅在终端中启动)
- sql-server - 如何将具有定义为 XML 模式的列的现有表迁移到具有相同定义的新数据库
- c# - 无法检索日期输入值 C# Web 窗体
- bison - 用空格分隔的表达式列表 + if/then/else 解决野牛语法中的冲突
- apache-kafka - 为什么我的简单 Kafka Consumer 示例不起作用
- floating-point - Fortran 中特定的双精度函数有用吗?
- numeric - cor(x, use = use) 中的错误:同时提供“x”和“y”或类似矩阵的“x”