简体   繁体   English

Rails 6 + Webpacker: Uncaught TypeError: $(...).select2 is not a function

[英]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.

相关问题 Rails Webpacker - 未捕获的类型错误:$(...).tooltip 不是 function - Rails Webpacker - Uncaught TypeError: $(…).tooltip is not a function Rails 6.1.3,webpacler,select2,出现错误:readyException.js:6 Uncaught TypeError: $(...).select2 is not a function - Rails 6.1.3, webpacler, select2, getting error: readyException.js:6 Uncaught TypeError: $(…).select2 is not a function Select2:Uncaught TypeError:options.results不是函数 - Select2: Uncaught TypeError: options.results is not a function jquery.min.js:2未捕获的TypeError:$(…).select2不是函数 - jquery.min.js:2 Uncaught TypeError: $(…).select2 is not a function Select2 匹配器不工作未捕获的类型错误 - Select2 Matcher not working Uncaught TypeError 未捕获的类型错误:$(...)[0].select 不是 function - Uncaught TypeError: $(...)[0].select is not a function 在select2中获取错误,使用文本错误选择下拉菜单Uncaught TypeError:d [b]不是函数 - Getting error in select2 make dropdown selected using text error Uncaught TypeError: d[b] is not a function 具有Select2的FormValidation未捕获的异常:未为Select2定义查询函数 - FormValidation with Select2 uncaught exception: query function not defined for Select2 未捕获的TypeError:对象[object Object]没有方法'select2' - Uncaught TypeError: Object [object Object] has no method 'select2' 未捕获的TypeError:无法读取select2中null的属性“ replace” - Uncaught TypeError: Cannot read property 'replace' of null in select2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM