Webpack Encore 和 jQuery 插件:$(...).isotope 不是函数

最大限度

我正在使用 Yarn 和 jQuery 进行 Symfony5 项目。当我尝试在Webpack Encore 中使用 jQuery 插件“ isotope-layout ”时出现错误。

我尝试修复它好几个小时,但我可能没有以正确的方式去做。

这是我的代码:

webpack.config.js

// Same error with or without "autoProvidejQuery"
Encore.autoProvidejQuery();

应用程序.js

import 'jquery';
import 'popper.js';
import 'bootstrap';
import 'isotope-layout';

// start the Stimulus application
import './bootstrap';

自定义.js

import jQuery from 'jquery';
import 'isotope-layout';

(function ($) {

    // Here is the error :
    $(...).isotope({...});

})(jQuery);

错误 :

Uncaught TypeError: $(...).isotope is not a function
斯蒂芬广场

使用 Isotope 时,您不必使用 jQuery。你可以从他们的 Webpack 文档中使用这个例子

var Isotope = require('isotope-layout');

var iso = new Isotope( '.grid', {
  // options...
});

如果您想使用 jQuery,还有一个示例。要安装它:

npm install jquery
npm install jquery-bridget

然后:

var $ = require('jquery');
var jQueryBridget = require('jquery-bridget');
var Isotope = require('isotope-layout');
// make Isotope a jQuery plugin
jQueryBridget( 'isotope', Isotope, $ );
// now you can use $().isotope()
$('.grid').isotope({
  // options...
});

如果您使用的是现代 Javascript(并且您使用的是 Webpack,太棒了!),在许多情况下您可能不需要 jQuery

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章