NodeJS with webpack jQuery.Deferred exception: o(…).select2 is not a function TypeError: o(…).select2 is not a function

和自甴很熟 提交于 2019-12-02 09:29:40

Took me a while to piece together what goes wrong here.

It comes down to that Select2 uses it's own loader and factory function to initialize itself, which doesn't get called by default. You need to call it manually.

If you have a window object and registered jQuery to the window object you can call Select2 as follows once in your main javascript file:

window.$ = window.jQuery = require('jquery);
require('select2')();

or if you prefer variables instead of calling the function on require directly:

window.$ = window.jQuery = require('jquery);
var select2Initialisator = require('select2');
select2Initialisator();

If you like to work with scopes or different versions of jQuery, you can also pass the jQuery instance you wish to register the select2 to, to select2 factory constructor as follows

 var jQuery = require('jquery');
 require('select2')(jQuery);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!