[英]Rails 6 + Webpacker: Uncaught TypeError: $(…).select2 is not a function
I'm trying to get select2 working in my app, but couldn't able to.我试图让select2在我的应用程序中工作,但不能。 I've added it by yarn add select2
.我已经通过yarn add select2
添加了它。 Below is my code下面是我的代码
javascript/packs/application.js javascript/packs/application.js
// This file is automatically compiled by Webpack, along with any other files
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.
require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
require("jquery")
require("bootstrap")
require("tempusdominus-bootstrap-4")
require("moment/locale/ja")
require('select2')
// stylesheets
require("../stylesheets/application.scss")
import "@fortawesome/fontawesome-free/css/all.css";
import "./owl.carousel.min.js";
import 'select2';
import 'select2/dist/css/select2.css';
$(document).ready(function(){
$('.customers-dropdown').select2();
$("select").change(function(event) {
$("#extra_price").html(formatCurrency($("#extra_" + $(".select").val()).html()));
computetotal(event);
});
$('#inlineDatepicker').datetimepicker({
inline: true,
sideBySide: false,
format: 'L',
useCurrent: false,
defaultDate: $('#weekStardate').val(),
});
$("#inlineDatepicker").on("change.datetimepicker", ({date, oldDate}) => {
var fdate = moment(date)
$('#weekStardate').val(fdate.format("D/M/YYYY"))
})
});
package.json package.json
{
"name": "rails_6_devise_example",
"private": true,
"dependencies": {
"@fortawesome/fontawesome-free": "^5.14.0",
"@rails/actioncable": "^6.0.0-alpha",
"@rails/activestorage": "^6.0.0-alpha",
"@rails/ujs": "^6.0.0-alpha",
"@rails/webpacker": "^5.2.1",
"bootstrap": "^4.5.2",
"bootstrap-datepicker": "^1.9.0",
"jquery": "^3.5.1",
"moment": "^2.27.0",
"node-sass": "^4.12",
"popper.js": "^1.16.1",
"rails-ujs": "^5.2.4-3",
"select2": "^4.1.0-rc.0",
"tempusdominus-bootstrap-4": "^5.1.2",
"turbolinks": "^5.2.0"
},
"version": "0.1.0",
"devDependencies": {
"webpack-dev-server": "^3.8.1"
}
}
I get Uncaught TypeError: $(...).select2 is not a function
.我得到Uncaught TypeError: $(...).select2 is not a function
。 I've looked into many posts and as suggested, I tried to add import $ from 'jquery';
我查看了很多帖子,按照建议,我尝试添加import $ from 'jquery';
above import 'select2';
以上import 'select2';
which works but that breaks datetimepicker resulting in Uncaught TypeError: jquery__WEBPACK_IMPORTED_MODULE_2___default(...)(...).datetimepicker is not a function
I don't have a clue what is happening.这有效,但会破坏 datetimepicker导致Uncaught TypeError: jquery__WEBPACK_IMPORTED_MODULE_2___default(...)(...).datetimepicker is not a function
我不知道发生了什么。
Any help is much appreciated, thanks!非常感谢任何帮助,谢谢!
I prepared the code quickly and it works fine.我快速准备了代码,它运行良好。 I just added this to the plugins:我刚刚将它添加到插件中:
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
moment: 'moment'
})
I also changed the order, because that is what the turbolink wanted;)我还更改了顺序,因为这是 turbolink 想要的;)
require("jquery")
require("bootstrap")
require("tempusdominus-bootstrap-4")
require("moment/locale/ja")
require("turbolinks").start()
require('select2')
Of course there are some bugs but I miss formatCurrency
but the most important things work:)当然有一些错误,但我想念formatCurrency
但最重要的事情有效:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.