如何使用ES6模块导入jQuery Masonry? [英] How do I import jQuery Masonry using ES6 modules?

查看:102
本文介绍了如何使用ES6模块导入jQuery Masonry?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此npm包 https://www.npmjs.com/package / masonry-layout

I am trying to use this npm package https://www.npmjs.com/package/masonry-layout

按照安装说明,我跑了:

As per install instruction I ran:

npm install masonry-layout --save

然后在我的文件中,

import '../../../node_modules/masonry-layout/dist/masonry.pkgd.min.js'

$('.blog-posts-container').masonry({
    // options ...
    itemSelector: '.card-blog',
    columnWidth: 200
})

我正在尝试在我的文件中导入包并运行它,但是我收到了这个错误:

I'm trying to import the package in my file and run it, but I get this error:

Uncaught TypeError: $(...).masonry is not a function

我认为我的导入存在问题。我做错了什么?

Im thinking there is something wrong with my import here. What am I doing wrong?

P.S。我正在使用webpack

P.S. I am using webpack

推荐答案

要使其正常工作,您需要使用 jQueryBridget ,它使jQuery插件脱离了构造函数。

To get this to work, you need to use jQueryBridget which makes jQuery plugin out of a constructor function.

以下是使用 imagesloaded plugin 的示例确保砌成图像加载后初始化。

Here's an example using the imagesloaded plugin to make sure masonry initializes after the images have loaded.

npm i -S jQueryBridget imagesLoaded

然后在 app.js (或其他):

import $ from 'jquery';
import Masonry from 'masonry-layout';
import jQueryBridget from 'jquery-bridget';
import imagesLoaded from 'imagesloaded';

jQueryBridget('masonry', Masonry, $);
jQueryBridget( 'imagesLoaded', imagesLoaded, $ );

const $container = $('#masonry-grid');

$container.imagesLoaded(() => {
  $container.masonry({
    columnWidth: 200,
    itemSelector: '.item',
  });
});

以上内容应捆绑(使用Webpack)到 bundle.js

The above should be bundled (using Webpack) into bundle.js.

为了完整起见,这里是HTML:

And for the sake of completeness, here's the HTML:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Masonry Madness</title>
</head>
<body>
  <div id="masonry-grid">
    <div class="item">
      <img src="https://s3.amazonaws.com/clarifai-img/demo/889/88940833.jpg">
    </div>
    <div class="item">
      <img src="https://s3.amazonaws.com/clarifai-img/demo/907/90775901.jpg">
    </div>
    <div class="item">
      <img src="https://s3.amazonaws.com/clarifai-img/demo/294/29489326.jpg">
    </div>
    <div class="item ">
      <img src="https://s3.amazonaws.com/clarifai-img/demo/100/100656385.jpg">
    </div>
    <div class="item ">
      <img src="https://s3.amazonaws.com/clarifai-img/demo/889/88940839.jpg">
    </div>
    <div class="item ">
      <img src="https://s3.amazonaws.com/clarifai-img/demo/111/111773987.jpg">
    </div>
    <div class="item ">
      <img src="https://s3.amazonaws.com/clarifai-img/demo/146/146371016.jpg">
    </div>
    <div class="item">
      <img src="https://s3.amazonaws.com/clarifai-img/demo/103/10313578.jpg">
    </div>
    <div class="item ">
      <img src="https://s3.amazonaws.com/clarifai-img/demo/554/55473337.jpg">
    </div>
    <div class="item ">
      <img src="https://s3.amazonaws.com/clarifai-img/demo/537/53727259.jpg">
    </div>
    <div class="item ">
      <img src="https://s3.amazonaws.com/clarifai-img/demo/111/111246515.jpg">
    </div>
    <div class="item">
      <img src="https://s3.amazonaws.com/clarifai-img/demo/461/46176355.jpg">
    </div>
  </div>

  <script src="bundle.js"></script>
</body>
</html>

HTH

这篇关于如何使用ES6模块导入jQuery Masonry?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆