Lightgallery 只能打开一次(VueJs)

Lightgallery can be opened only once (VueJs)

我有这个问题,lightgallery 只能打开一次。每次下一次单击按钮都没有响应。我正在使用 lightgallery 作为组件。

在我的父组件中,我有两个按钮,用于打开图片或视频库

ParentComponent.vue

<template>
  <div>
    <button class="btn-secondary" @click="showGallery('IMAGE')">
        {{ $t('showImages') }}
    </button>
    <button class="btn-secondary" @click="showGallery('VIDEO')">
        {{ $t('playVideo') }}
    </button>
    <LightGallery
       v-if="galleryVisible" :gallery-items="galleryItems"
       :gallery-type="galleryType" :user-subscription="userSubscription">
    </LightGallery>
  </div>
<template>

<script>
import LightGallery from "./subComponents/LightGallery.vue";

    export default {
        name: "Location",
        components: {
            LightGallery: LightGallery,
        },
        data() {
            return {
                // image / video slide show data
                locationImages: [],
                locationVideo: [],
                galleryItems: {},
                galleryVisible: false,
                galleryType: 'IMAGE',
            };
        },
        methods: {
            showGallery(type) {
                this.galleryItems = {};
                this.galleryVisible = true;
                this.galleryType = type;
                if (type === 'IMAGE') {
                    this.galleryItems = this.locationImages;
                } else if (type === 'VIDEO') {
                    this.galleryItems = this.locationVideo
                }
            },
            hideGallery() {
                this.galleryItems = {};
                this.galleryVisible = false;
            },
    };
</script>

这是我的子 (lightgallery) 组件

<template>
    <div id="lightgallery">
    </div>
</template>

<script>

    // Light gallery
    import 'lightgallery.js'
    import 'lightgallery.js/dist/css/lightgallery.css'
    import 'lg-zoom.js'
    import 'lg-autoplay.js'
    import 'lg-fullscreen.js'
    import 'lg-hash.js'
    import 'lg-pager.js'
    import 'lg-share.js'
    import 'lg-thumbnail.js'
    import 'lg-video.js'
    // Light gallery

    export default {
        name: "LightGallery",
        props: {
            galleryItems: {
                type: Array,
                required: true
            },
            galleryType: {
                type: String,
                required: true
            },
            userSubscription: {
                type: Object,
                required: false,
                default: {
                    active: false
                }
            }
        },
        methods: {
            openGallery() {
                // prepare images / video to display them in lightGallery
                let dynamicElements = [];
                if (this.galleryType === 'IMAGE') {
                    this.galleryItems.forEach(function (value) {
                        dynamicElements.push({
                            src: '/' + value.hd_path,
                            thumb: '/' + value.thumb_path,
                        });
                    });

                }
                if (this.galleryType === 'VIDEO') {
                    this.galleryItems.forEach(function (value) {
                        dynamicElements.push({
                            src: 'https://vimeo.com/' + value.vimeo_id,
                        });
                    });
                }

                let lg = document.getElementById('lightgallery');
                window.lightGallery(lg, {
                    mode: 'lg-slide',
                    download: false,
                    thumbnail: true,
                    dynamic: true,
                    dynamicEl: dynamicElements,
                    autoplayFirstVideo: true,
                    loadVimeoThumbnail: false,
                });

                lg.addEventListener('onCloseAfter', function (event, index, fromTouch, fromThumb) {
                    lg.data('lightGallery').destroy(true);
                }, false);

                window.lightGallery(lg);
            },
        },
        mounted() {
            this.openGallery();
        }
    };
</script>

问题! 在第一个 "refresh" 页面重新加载时,如果我单击按钮“显示图像”或“显示视频”,图库将打开并且运行良好。当我关闭图库时,我在开发者工具中看到错误(可能与我的问题完全无关)

接下来每次单击“显示图像”或“显示视频”按钮都不会执行任何操作。那么我怎样才能正确销毁 Lightgallery 以便能够重新打开它呢?如果您需要任何其他信息,请告诉我,我会提供。谢谢!

尝试将此行添加到您的 showGallery 方法中。

        showGallery(type) {

            this.galleryVisible = false // <<--- this line

            this.galleryItems = {};
            this.galleryVisible = true;
            this.galleryType = type;
            if (type === 'IMAGE') {
                this.galleryItems = this.locationImages;
            } else if (type === 'VIDEO') {
                this.galleryItems = this.locationVideo
            }
        },

Light Gallery 似乎只能工作一次。但是上面的 行代码 将重新创建 Light Gallery 组件,并且您每次单击时都会有一个新的 Light Gallery 组件实例(正是您在页面加载时拥有的实例)在将启动 Light Gallery 的按钮之一上。