【问题标题】:How to use lightgallery with Gatsby/React如何在 Gatsby/React 中使用 lightgallery
【发布时间】:2018-11-17 06:48:31
【问题描述】:

我正在尝试使用名为“lightgallery”的 jQuery 插件通过 Gatsby/React 构建一个图片库。我设法弄清楚如何让它在开发中工作。但是,当我尝试构建站点时,我遇到了问题。这是我的组件代码

import React, { Component } from 'react'
import Content from '../components/content-container/content'
import $ from 'jquery'
import 'lightgallery'
import 'lg-video'
import 'lg-zoom'

import img_1 from '../images/assets/1.jpg'
import img_2 from '../images/assets/2.jpg'
import img_3 from '../images/assets/3.jpg'

import './gallery.sass'

class Gallery extends Component {
  onLightGallery = node => {
    this.lightGallery = node
    $(node).lightGallery()
  }

  componentWillUnmount() {
    $(this.lightGallery)
      .data('lightGallery')
      .destroy(true)
  }

  render() {
    return (
      <Content>
        <Hero heroImage="hero hero-img-gallery">
          <h1 className="hero-title">Gallery</h1>
        </Hero>
        <div className="wrapper">
          <p className="title-gallery">Project video</p>
          <div className="lightgallery" ref={this.onLightGallery}>
            <figure href={img_1}>
              <img src={img_1} alt="moxon" />
              <figcaption>text</figcaption>
              <Play />
            </figure>
            <figure href={img_2}>
              <img src={img_2} alt="thumbnail" />
              <figcaption>text</figcaption>
            </figure>
            <figure href={img_3}>
              <img src={img_3} alt="thumbnail" />
              <figcaption>text</figcaption>
            </figure>
          </div>
          <p className="title-gallery">images</p>
          <div className="lightgallery" ref={this.onLightGallery}>
            <figure href={img_1}>
              <img src={img_1} alt="thumbnail" />
              <figcaption>text</figcaption>
            </figure>
            <figure href={img_2}>
              <img src={img_2} alt="thumbnail" />
              <figcaption>text</figcaption>
            </figure>
          </div>
        </div>
      </Content>
    )
  }
}

export default Gallery

一切都在开发中,但是,当我尝试“gatsby build”时,它会抛出以下错误:

  1340 |     };
  1341 |
> 1342 |     $.fn.lightGallery = function(options) {
       | ^
  1343 |         return this.each(function() {
  1344 |             if (!$.data(this, 'lightGallery')) {
  1345 |                 $.data(this, 'lightGallery', new Plugin(this, options));


  WebpackError: TypeError: Cannot set property 'lightGallery' of undefined

  - lightgallery.js:1342
    [lib]/[lightgallery]/dist/js/lightgallery.js:1342:1

  - lightgallery.js:1358
    [lib]/[lightgallery]/dist/js/lightgallery.js:1358:2

  - lightgallery.js:8 Object.<anonymous>
    [lib]/[lightgallery]/dist/js/lightgallery.js:8:1

  - lightgallery.js:9 ./node_modules/lightgallery/dist/js/lightgallery.js
    [lib]/[lightgallery]/dist/js/lightgallery.js:9:6

  - lightgallery.js:18 Object../node_modules/lightgallery/dist/js/lightgallery.js
    [lib]/[lightgallery]/dist/js/lightgallery.js:18:2

  - bootstrap:19 __webpack_require__
    lib/webpack/bootstrap:19:1


  - bootstrap:19 __webpack_require__
    lib/webpack/bootstrap:19:1

  - sync-requires.js:10 Object../.cache/sync-requires.js
    lib/.cache/sync-requires.js:10:53

  - bootstrap:19 __webpack_require__
    lib/webpack/bootstrap:19:1

  - static-entry.js:9 Module../.cache/static-entry.js
    lib/.cache/static-entry.js:9:22

  - bootstrap:19 __webpack_require__
    lib/webpack/bootstrap:19:1

  - bootstrap:83
    lib/webpack/bootstrap:83:1


  - universalModuleDefinition:3 webpackUniversalModuleDefinition
    lib/webpack/universalModuleDefinition:3:1

  - universalModuleDefinition:10 Object.<anonymous>
    lib/webpack/universalModuleDefinition:10:2

我不知道出了什么问题以及如何解决这个问题,因为我是 gatsby/react 和 webpack 的新手。任何指针将不胜感激!

【问题讨论】:

    标签: jquery reactjs webpack gatsby lightgallery


    【解决方案1】:

    您遇到此问题是因为 lightgallery 代码正在服务器上运行,而 window 和 DOM 的其他部分不可用。你可以learn more about how to debug and workaround this in the official Gatsby documentation here

    该资源建议将第三方库排除在服务器端评估之外:

    exports.onCreateWebpackConfig = ({ stage, loaders, actions }) => {
      if (stage === "build-html") {
        actions.setWebpackConfig({
          module: {
            rules: [
              {
                test: /bad-module/,
                use: loaders.null(),
              },
            ],
          },
        })
      }
    }
    

    【讨论】:

    • @codreyward 感谢您的回复。我想我现在明白了这个问题,但是,我不知道如何使用上面的示例代码来排除 lightgallery 库。你能示范怎么做吗?我对 Gatsby 和 Webpack 还很陌生,更详细的代码示例会很有帮助!
    • @YorkWang 如果您查看链接的文档,它可能会有所帮助。我还要注意,这将进入您的 gatsby-node.js 文件(您可能还没有),并且您希望将 /bad-module/ 表达式替换为与您要排除的库匹配的表达式(例如 @987654326 @)。
    • 非常感谢!!有效!如果可以的话,我还有最后一个问题。我有两个需要排除的插件,一个是lightgallery,另一个是lg-video,它是一个单独的lightgallery插件。我试图在 /lightgallery/ 之后添加 /lg-video/,这似乎破坏了构建过程?再次感谢您的帮助!
    • 您可以在正则表达式中组合它们:/(lightgallery|lg-video)/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-03
    • 1970-01-01
    • 2018-09-28
    • 1970-01-01
    • 2023-03-10
    • 2021-03-29
    相关资源
    最近更新 更多