我正在修改我的应用程序以使用nodejs并通过gulp进行browserify来生成一个缩小的js。
我已经从手动加载依赖项并手动更新切换为使用 npm 安装它们。
一切都很顺利,但是当我想安装 select2 时,它开始到处抛出错误。当我移动已删除的 npm 手动更新文件时required()
file.
jquery.js:3841 jQuery.Deferred 异常:o(...).select2 不是函数 TypeError: o(...).select2 不是函数
在 i.init (https://www.example.com/newstyle/js/closure.js?time=1559747711:1:5612 https://www.example.com/newstyle/js/closure.js?time=1559747711:1:5612)
在 i.sysInit (https://www.example.com/newstyle/js/closure.js?time=1559747711:1:108153 https://www.example.com/newstyle/js/closure.js?time=1559747711:1:108153)
在我(https://www.example.com/newstyle/js/closure.js?time=1559747711:1:106602 https://www.example.com/newstyle/js/closure.js?time=1559747711:1:106602)
在新的我(https://www.example.com/newstyle/js/closure.js?time=1559747711:1:5333 https://www.example.com/newstyle/js/closure.js?time=1559747711:1:5333)
在 HTMLSelectElement 处。 (https://www.example.com/newstyle/js/closure.js?time=1559747711:1:108496 https://www.example.com/newstyle/js/closure.js?time=1559747711:1:108496)
在 Function.each (https://www.example.com/newstyle/js/closure.js?time=1559747711:1:200628 https://www.example.com/newstyle/js/closure.js?time=1559747711:1:200628)
在 _.fn.init.each (https://www.example.com/newstyle/js/closure.js?time=1559747711:1:199273 https://www.example.com/newstyle/js/closure.js?time=1559747711:1:199273)
at _.fn.init.d.fn.(匿名函数) [作为 FormDropdownHandler] (https://www.example.com/newstyle/js/closure.js?time=1559747711:1:108384 https://www.example.com/newstyle/js/closure.js?time=1559747711:1:108384)
在 HTML 文档中。 (https://www.example.com/newstyle/js/closure.js?time=1559747711:1:108696 https://www.example.com/newstyle/js/closure.js?time=1559747711:1:108696)
在 HTMLDocument.dispatch (https://www.example.com/newstyle/js/closure.js?time=1559747711:1:240241 https://www.example.com/newstyle/js/closure.js?time=1559747711:1:240241) 不明确的
dropdown.module.js:53 Uncaught TypeError: o(...).select2 不是函数
在 i.init (dropdown.module.js:53)
在 i.sysInit (oc.foundation.base.js:157)
在我 (oc.foundation.base.js:20)
在新的 i (dropdown.module.js:19)
在 HTMLSelectElement 处。 (oc.foundation.base.js:191)
在 Function.each (jquery.js:367)
在 _.fn.init.each (jquery.js:202)
at _.fn.init.d.fn.(/匿名函数) [作为 FormDropdownHandler] (https://www.example.com/newstyle/js/closure.js?time=1559747711:1:108384 https://www.example.com/newstyle/js/closure.js?time=1559747711:1:108384)
在 HTML 文档中。 (oc.foundation.base.js:213)
在 HTMLDocument.dispatch (jquery.js:5237)
我正在使用的代码是:
<select name="pickup_point">
<option value="1" >all work</option>
<option value="4" >no play</option>
<option value="5" >dull boy</option>
</select>
和 JavaScript:
$ = require('jquery');
require('select2');
$(document).ready(function(){
$('select').select2();
});
当我在 index.js 文件中需要 select2 时,如何才能使其工作?
我花了一段时间才拼凑出这里出了什么问题。
归根结底,Select2 使用它自己的加载器和工厂函数来初始化自身,默认情况下不会调用它。您需要手动调用它。
如果您有一个窗口对象并将 jQuery 注册到该窗口对象,您可以按如下方式调用 Select2once在你的主 javascript 文件中:
window.$ = window.jQuery = require('jquery);
require('select2')();
或者如果您更喜欢变量而不是直接在 require 上调用函数:
window.$ = window.jQuery = require('jquery);
var select2Initialisator = require('select2');
select2Initialisator();
如果您喜欢使用作用域或不同版本的 jQuery,您还可以将要注册 select2 的 jQuery 实例传递给 select2 工厂构造函数,如下所示
var jQuery = require('jquery');
require('select2')(jQuery);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)