You can activate Lightgallery just one time in VueJs

I am facing an issue where lightgallery can only be opened once. Subsequent clicks on the button are unresponsive. The lightgallery is being used as a component.

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

This is my child component for 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() {
                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 Issue! Upon initial page load, when I click on the Show images or Play video buttons, the gallery opens and functions correctly. However, upon closing the gallery, I encounter an error in the developer tools (which may be unrelated to my issue).

https://i.sstatic.net/59Twn.png

Subsequent clicks on the Show images or Play video buttons do not trigger any action. How can I properly destroy Lightgallery so that I can reopen it? If you require additional information, please let me know and I will provide it. Thank you!

Answer №1

For an enhanced user experience, consider implementing this modification within your showGallery function.

        showGallery(type) {

            this.galleryVisible = false // <<--- implement 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
            }
        },

It has been observed that Light Gallery functionality may be limited to a single use. By incorporating the aforementioned line of code, you can refresh the Light Gallery Component and achieve a new instance upon each interaction with the activation buttons associated with Light Gallery.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for efficiently utilizing mapActions in vue with Typescript class components!

Can someone please provide guidance on the correct way to use ...mapActions([]) within a Typescript vue class component? This is my current approach: <script lang="ts"> import { Component, Prop, Vue } from "vue-property-decorator"; import { mapActi ...

Issue with binding Vue v-model to input element in Parent component is not functioning as expected

Utilizing Vue, Nuxt, and Vue Good Table to implement live searching on a table has presented a challenge for me. I created a child component called child.vue and imported it into the parent page named parent.vue. To bind the searchTerm with the input eleme ...

Cleve js does not include delimiters in its date output

In my Vue.js project, I decided to implement a date input field using cleave.js. After passing the following options: options="{date: true, delimiter: '/', datePattern: ['m', 'd', 'Y']}" I encountered an issue wh ...

Creating a React component dynamically and applying unique custom properties

I'm currently in the process of refactoring my React code to enhance its usability in situations where direct use of Babel is not possible, such as in short snippets of JavaScript embedded on web pages. As part of this refactor, I am setting up a conc ...

How can you refresh the source element?

Is there a way to make the browser reload a single element on the page (such as 'src' or 'div')? I have tried using this code: $("div#imgAppendHere").html("<img id=\"img\" src=\"/photos/" + recipe.id + ".png\" he ...

Having trouble setting a value for a textbox in angularjs

Greetings! I am currently working on a web application using AngularJS. My task involves binding data from various API's to a textbox in my project. Below is the snippet of the HTML code where I attempt to achieve this: <input class="with-icon" ty ...

Updating a React event as it changes with each onChange event

Let's address a disclaimer before diving into the issue - for a quick look, visit this pen and type something there. The Scenario This is the JSX code snippet used in my render method: <input value={this.state.value} onChange={this.handleCh ...

The Angular web application utilizing an ASP.NET Web API that is hosted on AppHarbor is showing the incorrect webpage

Working on a web application using Angular and ASP.NET Web API in Visual Studio 2015 has been quite the journey for me. Upon running the solution in VS2015, I encountered two tabs opening in Chrome - one for my Angular client-side application and another ...

How can you switch between CSS styles using JQuery?

Is there a way to change CSS properties every time I click using jQuery? I couldn't find any information on this specific topic. Can someone guide me on how to achieve this with jQuery? I want the background color to change each time it is clicked. W ...

Unable to display nested JSON data from API in Vue.js

Having trouble accessing nested properties from API JSON data. The Vue component I'm working on: var profileComponent = { data : function() { return { isError : false, loading : true, users : null, ...

JavaScript: Manipulating Data with Dual Arrays of Objects

//Original Data export const data1 = [ { addKey: '11', address: '12', value: 0 }, { addKey: '11', address: '12', value: 0 }, { addKey: '12', address: '11', value: 0 }, { addKey: &a ...

Utilizing the 'container' property in a React.js React-Bootstrap modal

How can I open a modal within a designated container using the native property "container"? Whenever I specify the class name of the container element, I encounter an error TypeError: Cannot use 'in' operator to search for 'current' in ...

Is there a way to set vue-awesome-swiper to loop seamlessly and show the pagination as well?

Recently, I implemented vue-awesome-swiper in a project. Below are the codes from the HomeSwiper.vue file: //HTML <template> <swiper :options="swiperOption" ref="mySwiper"> <swiper-slide v-for="(item,index) in banners" :key="inde ...

Steps for adding transitions to Font Awesome 5 Vue Icons

I am trying to replace an icon and apply a transition effect. By using the vue-fontawesome npm package, I have configured my icon with computedIcon, which is a computed property that determines the icon to be displayed: <transition name="fade"> ...

I am currently using jQuery autocomplete in an attempt to display the value in the textbox as a table. The data is not being retrieved from a JSON source using Ajax directly in the text

Attempting ajax with json for autocomplete using Jquery for the first time, aiming for auto-complete appearance but table-like structure. Below is the jquery code snippet: $("document").ready(function (){ $(function () { $.ajax({ url: "dum ...

Simple HTML generator using Node.js

My preference is to write my HTML in a basic and straightforward manner, without any fancy tools. The only feature I would like to have is the ability to use an include statement to easily add header, navigation, and footer sections to each page. I'v ...

Methods to Exclude api_key from URL in AngularJS

To make a GET request to a REST API, I require an apikey. The request will be formed like this - $http.get() The response from the API will be in JSON format. However, for security reasons, I don't want the api key to be visible in the URL. Is ther ...

I encountered an error stating that there was no 'Access-Control-Allow-Origin' header present on the requested resource

When I attempted to make a request from my @vue/cli 4.5.8 app to my Laravel (Laravel Framework 8.24.0) backend hosted at , I encountered an error in the console: Access to XMLHttpRequest at 'http://local-backend-currencies.com/login' from origin ...

Can a single file in NextJS 13 contain both client and server components?

I have a component in one of my page.tsx files in my NextJS 13 app that can be almost fully rendered on the server. The only client interactivity required is a button that calls useRouter.pop() when clicked. It seems like I have to create a new file with ...

Tips for eliminating choices upon button press

How do I remove nested options inside a select element upon button click? I'm not sure where my mistake is, could it be in the for loop? var select = document.getElementById('colorSelect'); var options = document.getElementsByTagName(&ap ...