首页 > 解决方案 > Lightgallery 只能打开一次(VueJs)

问题描述

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

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

父组件.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>

问题!如果我单击按钮显示图像或显示视频,则在第一次“刷新”页面重新加载时,图库会打开并且效果很好。当我关闭画廊时,我在开发人员工具中看到错误(可能与我的问题完全无关)

画廊错误

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

标签: javascriptvue.jsvuejs2lightgallery

解决方案


尝试将此行添加到您的 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 的按钮之一时,您将拥有一个新的 Light Gallery 组件实例(正是您在页面加载时所拥有的)画廊。


推荐阅读