javascriptvue.jsvuejs2lightgallery

Lightgallery can be opened only once (VueJs)


I have this problem, that lightgallery can be opened only once. Each next click on button is unresponsive. I'm using lightgallery as component.

In my parent component I have two buttons, for opening image or video gallery

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>

And this is my child (lightgallery) component

<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>

The problem! On first "refresh" page reload if I click on button Show image or show video, gallery opens and it works well. When I close gallery I see error in developer tools (maybe complletly unrelated, with my problem)

gallery error

Each next click on buttons Show image or show video does nothing. So how can I properly destroy Lightgallery so I will be able to reopen it? If you need any additional informations, please let me know and I will provide. Thank you!


Solution

  • Try adding this line to your showGallery method.

            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 only seems to work once. But the above line of code will re-create the Light Gallery Component and you'll have a fresh instance of Light Gallery component (exactly what you have on a page load) each time you click on one of the buttons that would initiate the Light Gallery.