如何将select2与webpack一起使用?
问题描述:
当我使用此代码要求select2( https://时,我正在使用webpack来管理我的所有资产. github.com/select2/select2 )我收到了错误
I'm using webpack to manage all my assets, when I use this code to require select2 (https://github.com/select2/select2) I got the error
$(...).select2不起作用.
$(...).select2 is not function.
require.ensure(['./vendors/select2'],function (require) {
require('./site');
});
// site.js
(function ($) {
$(document).ready(function () {
$(".js-1example-basic-single").select2();
});
})(jQuery);
我认为模块导出有问题. 我尝试了很多搜索,但没有希望.
I think there is something wrong with module export. I tried many search but no hope.
任何人都请告诉我该怎么办,这花了我大约10个小时.
Anyone please tell me what to do, It took me about 10 hours.
谢谢!
答
您可以通过以下方式运行select2
:
You can run select2
in this way:
import $ from 'jquery';
import 'select2'; // globally assign select2 fn to $ element
import 'select2/dist/css/select2.css'; // optional if you have css loader
$(() => {
$('.select2-enable').select2();
});